Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Draw defaults for app from package.json #64

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"lodash.defaultsdeep": "^4.6.1",
"lodash.get": "^4.4.2",
"lodash.isequal": "^4.5.0",
"lodash.pick": "^4.4.0",
"lodash.set": "^4.3.2",
"node-fetch": "^2.6.0",
"node-yaml": "^4.0.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/cli-app/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
//

export const APP_CONFIG_FILENAME = 'app.yml';
export const PACKAGE_JSON_FILENAME = 'package.json';
export const APP_TYPE = 'wrn:app';
export const DEFAULT_PORT = 5999;
export const BASE_URL = '/app';

export const DEFAULT_PACKAGE_JSON_ATTRIBUTES = ['name', 'version', 'author', 'license', 'description',
'keywords', 'homepage', 'repository', 'bugs'];
19 changes: 6 additions & 13 deletions packages/cli-app/src/handlers/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,19 @@

import { spawnSync } from 'child_process';
import semverInc from 'semver/functions/inc';
import { log } from '@dxos/debug';

import { readFile } from '@dxos/cli-core';

import { APP_CONFIG_FILENAME } from '../config';
import { log } from '@dxos/debug';

const DEFAULT_BUILD = 'yarn webpack -p';
import { loadAppConfig } from './config';

export const build = (config, { getAppRecord }) => async ({ verbose }) => {
const appConfig = await readFile(APP_CONFIG_FILENAME);

const {
build = DEFAULT_BUILD
} = appConfig;
const conf = await loadAppConfig();

const record = getAppRecord(appConfig);
record.version = semverInc(appConfig.version, 'patch');
const record = getAppRecord(conf);
record.version = semverInc(conf.version, 'patch');

log(`Building ${record.name}...`);
const [command, ...args] = build.split(' ');
const [command, ...args] = conf.build.split(' ');

// build with configuration
const { status } = spawnSync(command, args, {
Expand Down
31 changes: 19 additions & 12 deletions packages/cli-app/src/handlers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@
// Copyright 2020 DxOS.
//

import assert from 'assert';
import fs from 'fs';
import os from 'os';
import { read, write } from 'node-yaml';
import defaultsDeep from 'lodash.defaultsdeep';
import pick from 'lodash.pick';

const update = config => async argv => {
const { conf } = argv;
import { readFile, writeFile } from '@dxos/cli-core';

const configFile = config.get('cli.app.serve.config', '').replace('~', os.homedir());
assert(configFile && fs.existsSync(configFile), 'Configuration file does not exist.');
import { APP_CONFIG_FILENAME, DEFAULT_PACKAGE_JSON_ATTRIBUTES, PACKAGE_JSON_FILENAME } from '../config';

let appConfig = await read(configFile);
appConfig = defaultsDeep({}, conf, appConfig);
const DEFAULT_BUILD = 'yarn webpack -p';

await write(configFile, appConfig);
export const updateAppConfig = async config => {
let appConfig = fs.existsSync(APP_CONFIG_FILENAME) ? await readFile(APP_CONFIG_FILENAME) : {};
appConfig = defaultsDeep({}, appConfig, config);

await writeFile(appConfig, APP_CONFIG_FILENAME);
};

export default {
update
export const loadAppConfig = async () => {
const packageProperties = pick(fs.existsSync(PACKAGE_JSON_FILENAME)
? await readFile(PACKAGE_JSON_FILENAME) : {}, DEFAULT_PACKAGE_JSON_ATTRIBUTES);
const appConfig = fs.existsSync(APP_CONFIG_FILENAME) ? await readFile(APP_CONFIG_FILENAME) : {};

return {
build: DEFAULT_BUILD,
...packageProperties,
...appConfig
};
};
2 changes: 1 addition & 1 deletion packages/cli-app/src/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export * from './build';
export * from './publish';
export * from './register';
export * from './query';
export { default as configuration } from './config';
export * from './config';
export { default as serve } from './serve';
18 changes: 8 additions & 10 deletions packages/cli-app/src/handlers/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
import assert from 'assert';
import IpfsHttpClient, { globSource } from 'ipfs-http-client';
import path from 'path';
import semverInc from 'semver/functions/inc';
import set from 'lodash.set';
import pify from 'pify';
import folderSize from 'get-folder-size';
import cliProgress from 'cli-progress';

import { log } from '@dxos/debug';
import { readFile, writeFile } from '@dxos/cli-core';

import { APP_CONFIG_FILENAME } from '../config';
import { loadAppConfig, updateAppConfig } from './config';

const getFolderSize = pify(folderSize);

const DEFAULT_DIST_PATH = 'dist';

export const publish = config => async ({ timeout, path: distPath = DEFAULT_DIST_PATH }) => {
const appConfig = await readFile(APP_CONFIG_FILENAME);
log(`Publishing ${appConfig.name}...`);
const conf = await loadAppConfig();

log(`Publishing ${conf.name}...`);

const ipfsServer = config.get('services.ipfs.server');
assert(ipfsServer, 'Invalid IPFS Server.');
Expand All @@ -32,7 +31,7 @@ export const publish = config => async ({ timeout, path: distPath = DEFAULT_DIST
timeout: timeout || '10m'
});

const publishFolder = path.join(process.cwd(), appConfig.publish || distPath);
const publishFolder = path.join(process.cwd(), conf.publish || distPath);

const source = globSource(publishFolder, { recursive: true });
const total = await getFolderSize(publishFolder);
Expand All @@ -49,9 +48,8 @@ export const publish = config => async ({ timeout, path: distPath = DEFAULT_DIST
const cid = addResult.cid.toString();

// Update CID in app.yml.
set(appConfig, 'package["/"]', cid);
appConfig.version = semverInc(appConfig.version, 'patch');
await writeFile(appConfig, APP_CONFIG_FILENAME);
set(conf, 'package["/"]', cid);
await updateAppConfig(conf);

log(`Published ${appConfig.name}@${appConfig.version} with cid ${cid}`);
log(`Published ${conf.name}@${conf.version} with cid ${cid}`);
};
16 changes: 4 additions & 12 deletions packages/cli-app/src/handlers/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

import assert from 'assert';
import clean from 'lodash-clean';
import isEqual from 'lodash.isequal';

import { readFile, writeFile, getGasAndFees } from '@dxos/cli-core';
import { getGasAndFees } from '@dxos/cli-core';
import { log } from '@dxos/debug';
import { Registry } from '@wirelineio/registry-client';

import { APP_CONFIG_FILENAME } from '../config';
import { loadAppConfig, updateAppConfig } from './config';

export const register = (config, { getAppRecord }) => async (argv) => {
const { verbose, version, namespace, 'dry-run': noop, txKey } = argv;
Expand All @@ -22,13 +21,8 @@ export const register = (config, { getAppRecord }) => async (argv) => {
assert(bondId, 'Invalid WNS Bond ID.');
assert(chainId, 'Invalid WNS Chain ID.');

const { names = [], ...appConfig } = await readFile(APP_CONFIG_FILENAME);
const { name = names } = argv;

assert(Array.isArray(name), 'Invalid App Record Name.');

const conf = {
...appConfig,
...await loadAppConfig(),
...clean({ version })
};

Expand All @@ -49,9 +43,7 @@ export const register = (config, { getAppRecord }) => async (argv) => {

let appId;
if (!noop) {
if (!isEqual(conf, appConfig)) {
await writeFile(conf, APP_CONFIG_FILENAME);
}
await updateAppConfig(conf);
const result = await registry.setRecord(userKey, record, txKey, bondId, fee);
appId = result.data;
log(`Record ID: ${appId}`);
Expand Down
18 changes: 1 addition & 17 deletions packages/cli-app/src/modules/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { asyncHandler } from '@dxos/cli-core';
import { log } from '@dxos/debug';

import { APP_TYPE, DEFAULT_PORT, BASE_URL } from '../config';
import { build, configuration, publish, register, query, serve } from '../handlers';
import { build, publish, register, query, serve } from '../handlers';

/**
* @param {object} config
Expand Down Expand Up @@ -132,21 +132,5 @@ export const AppModule = ({ config }) => {
handler: asyncHandler(serve.stop(config))
})
})

// Configuration.
.command({
command: ['config'],
describe: 'Applications configuration',
builder: yargs => yargs
// update.
.command({
command: ['update'],
describe: 'Update applications configuration.',
builder: yargs => yargs.version(false)
.option('conf', { type: 'json' }),

handler: asyncHandler(configuration.update(config))
})
})
});
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10011,6 +10011,11 @@ lodash.omit@^4.5.0:
resolved "https://registry.yarnpkg.com/lodash.omit/-/lodash.omit-4.5.0.tgz#6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60"
integrity sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA=

lodash.pick@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=

lodash.pickby@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.pickby/-/lodash.pickby-4.6.0.tgz#7dea21d8c18d7703a27c704c15d3b84a67e33aff"
Expand Down