Skip to content

Commit

Permalink
Merge pull request #266 from alibaba/fix-cli-default-config-name
Browse files Browse the repository at this point in the history
fix(@dawnjs/cli): fix deefault config path name
  • Loading branch information
soulwu authored Jul 9, 2021
2 parents 519b63e + 4a8cd29 commit be5b270
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
12 changes: 5 additions & 7 deletions packages/cli/lib/common/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
*/

const path = require('path');
const pkg = require('../../package');
const ENV = process.env;

exports.homePath = function () {
return ENV.HOME || ENV.USERPROFILE || (ENV.HOMEDRIVE + ENV.HOMEPATH);
return ENV.HOME || ENV.USERPROFILE || ENV.HOMEDRIVE + ENV.HOMEPATH;
};

exports.dataPath = function () {
return ENV.HOME || ENV.APPDATA || ENV.LOCALAPPDATA ||
ENV.TMPDIR || ENV.TEMP;
return ENV.HOME || ENV.APPDATA || ENV.LOCALAPPDATA || ENV.TMPDIR || ENV.TEMP;
};

exports.storePath = function () {
return path.normalize(`${this.dataPath()}/.${pkg.name}`);
return path.normalize(`${this.dataPath()}/.dawn`);
};

exports.rcPath = function () {
return `${this.homePath()}/.${pkg.name}rc`;
};
return `${this.homePath()}/.dawnrc`;
};
26 changes: 13 additions & 13 deletions packages/cli/lib/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const paths = require('./common/paths');
const FETCH_TIMEOUT = 30000;

exports.setLocalRc = async function (name, value) {
const rcObject = await this.getLocalRc() || {};
const rcObject = (await this.getLocalRc()) || {};
rcObject[name] = value;
const text = yaml.stringify(rcObject);
const rcFile = paths.rcPath();
Expand All @@ -39,14 +39,14 @@ exports.getLocalRc = async function (name) {
};

exports.getRemoteRc = async function (name) {
const remoteRCConf = await this.getRemoteConf('rc') || {};
const remoteRCConf = (await this.getRemoteConf('rc')) || {};
const value = name ? remoteRCConf[name] || '' : remoteRCConf;
debug('getRemoteRc', name || 'all', value || '<null>');
return utils.isString(value) ? value.trim() : value;
};

exports.getProjectRc = async function (name) {
const configsPath = path.resolve(process.cwd(), `./.${pkg.name}`);
const configsPath = path.resolve(process.cwd(), './.dawn');
const configs = utils.confman.load(configsPath);
const rcObject = configs.rc || {};
const value = name ? rcObject[name] || '' : rcObject;
Expand All @@ -56,21 +56,21 @@ exports.getProjectRc = async function (name) {

exports.getRc = async function (name, opts) {
opts = Object.assign({}, opts);
const value = await this.getProjectRc(name) ||
await this.getLocalRc(name) ||
(opts.remote !== false && await this.getRemoteRc(name)) ||
pkg.configs[name] || '';
const value =
(await this.getProjectRc(name)) ||
(await this.getLocalRc(name)) ||
(opts.remote !== false && (await this.getRemoteRc(name))) ||
pkg.configs[name] ||
'';
debug('getRc', name, value || '<null>');
return utils.isString(value) ? value.trim() : value;
};

exports.getServerUri = async function () {
const serverUri = await this.getProjectRc('server') ||
await this.getLocalRc('server') ||
pkg.configs.server || '';
const serverUri =
(await this.getProjectRc('server')) || (await this.getLocalRc('server')) || pkg.configs.server || '';
debug('getServerUri', serverUri || '<null>');
return utils.isString(serverUri) ?
trim(serverUri.trim(), '/') : '';
return utils.isString(serverUri) ? trim(serverUri.trim(), '/') : '';
};

exports.getRemoteConf = async function (name, defaultValue = {}) {
Expand Down Expand Up @@ -109,4 +109,4 @@ exports.setLocalConf = async function (name, value) {
const filename = path.normalize(`${localStoreDir}/${name}.json`);
const text = JSON.stringify(value);
await writeFile(filename, text);
};
};
25 changes: 9 additions & 16 deletions packages/cli/lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const stp = require('stp');
const debug = require('debug')('context');

class Context extends EventEmitter {

constructor(cli, opts) {
super();
opts = opts || {};
Expand All @@ -34,18 +33,15 @@ class Context extends EventEmitter {
this.console = console;
this.utils = utils;
this.inquirer = utils.inquirer;
this.configName = `./.${pkg.name}`;
this.configName = './.dawn';
this.configPath = path.resolve(this.cwd, this.configName);
this.conf = configs;
this.mod = mod;
}

async configIsExists() {
debug('config name', this.configName);
const files = await utils.globby([
`${this.configName}/**/*.*`,
`${this.configName}.*`
]);
const files = await utils.globby([`${this.configName}/**/*.*`, `${this.configName}.*`]);
debug('config files', files);
return files.length > 0;
}
Expand Down Expand Up @@ -119,18 +115,17 @@ class Context extends EventEmitter {
const middleware = middlewares.shift();
if (!middleware) return;
const handler = await this.load(middleware);
const next = (args) => {
const next = args => {
if (next.__result) return next.__result;
next.__result = this._execQueue(middlewares, args, onFail)
.catch(err => onFail(err));
next.__result = this._execQueue(middlewares, args, onFail).catch(err => onFail(err));
return next.__result;
};
return handler.call(this, next, this, args);
}

unescapeExpr(str) {
if (!str) return str;
return str.replace(/\\\{/, '{').replace('/\\\}/', '}');
return str.replace(/\\\{/, '{').replace('/\\}/', '}');
}

_parseOpts(opts) {
Expand All @@ -156,9 +151,9 @@ class Context extends EventEmitter {
throw new Error('Invalid pipeline config');
}
opts = this._parseOpts(opts);
const modFactory = opts.location ?
require(path.resolve(this.cwd, opts.location)) :
await middleware.require(opts.name, this.cwd);
const modFactory = opts.location
? require(path.resolve(this.cwd, opts.location))
: await middleware.require(opts.name, this.cwd);
if (!utils.isFunction(modFactory)) {
throw new Error(`Invalid middleware '${opts.name}'`);
}
Expand All @@ -171,8 +166,7 @@ class Context extends EventEmitter {
middlewares.push((next, ctx, args) => {
resolve(args);
});
this._execQueue(middlewares, initailArgs, reject)
.catch(err => reject(err));
this._execQueue(middlewares, initailArgs, reject).catch(err => reject(err));
});
}

Expand All @@ -186,7 +180,6 @@ class Context extends EventEmitter {
}
return this.exec(this.pipeline);
}

}

module.exports = Context;

0 comments on commit be5b270

Please sign in to comment.