Skip to content

Commit

Permalink
style: prettier format
Browse files Browse the repository at this point in the history
  • Loading branch information
cpselvis committed May 26, 2020
1 parent 8516cae commit d1a09d1
Show file tree
Hide file tree
Showing 35 changed files with 1,903 additions and 1,625 deletions.
6 changes: 6 additions & 0 deletions packages/feflow-cli/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
trailingComma: 'none',
tabWidth: 2,
semi: true,
singleQuote: true,
};
1 change: 1 addition & 0 deletions packages/feflow-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"mocha": "^6.2.0",
"nodemon": "^1.19.1",
"nyc": "^14.1.1",
"prettier": "2.0.5",
"ts-node": "^8.3.0",
"typescript": "^3.5.2"
},
Expand Down
82 changes: 54 additions & 28 deletions packages/feflow-cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,60 @@ const pkg = require('../../package.json');

const checkNodeVersion = (wanted: any, id: string) => {
if (!semver.satisfies(process.version, wanted)) {
console.log(chalk.red(
'You are using Node ' + process.version + ', but this version of ' + id +
' requires Node ' + wanted + '.\nPlease upgrade your Node version.'
));
console.log(
chalk.red(
'You are using Node ' +
process.version +
', but this version of ' +
id +
' requires Node ' +
wanted +
'.\nPlease upgrade your Node version.'
)
);
process.exit(1);
}
}
};

const handleError = (err: any) => {
if (err) {
console.log(chalk.red(err));
}
process.exit(2);
}
};

const printBanner = () => {
figlet.text('feflow', {
font: '3D-ASCII',
horizontalLayout: 'default',
verticalLayout: 'default'
}, function (err, data: any) {
if (err) {
handleError(err);
}
figlet.text(
'feflow',
{
font: '3D-ASCII',
horizontalLayout: 'default',
verticalLayout: 'default'
},
function (err, data: any) {
if (err) {
handleError(err);
}

console.log(chalk.green(data));
console.log(chalk.green(` Feflow,current version: v${pkg.version}, homepage: https://github.com/Tencent/feflow `));
console.log(chalk.green(' (c) powered by Tencent, aims to improve front end workflow. '));
console.log(chalk.green(' Run fef --help to see usage. '));
});
}
console.log(chalk.green(data));
console.log(
chalk.green(
` Feflow,current version: v${pkg.version}, homepage: https://github.com/Tencent/feflow `
)
);
console.log(
chalk.green(
' (c) powered by Tencent, aims to improve front end workflow. '
)
);
console.log(
chalk.green(
' Run fef --help to see usage. '
)
);
}
);
};

export default function entry() {
const args = minimist(process.argv.slice(2));
Expand All @@ -64,8 +87,8 @@ export default function entry() {
}

if (!cmd && !args.h && !args.help) {
printBanner();
return;
printBanner();
return;
}

return feflow.init(cmd).then(() => {
Expand All @@ -86,12 +109,15 @@ export default function entry() {
feflow.hook.emit(HOOK_TYPE_BEFORE);

feflow.hook.on(EVENT_COMMAND_BEGIN, () => {
return feflow.call(cmd, feflow).then(() => {
feflow.hook.emit(HOOK_TYPE_AFTER);
logger.debug(`call ${cmd} success`);
}).catch((err) => {
handleError(err);
});
return feflow
.call(cmd, feflow)
.then(() => {
feflow.hook.emit(HOOK_TYPE_AFTER);
logger.debug(`call ${cmd} success`);
})
.catch((err) => {
handleError(err);
});
});
});
}
4 changes: 1 addition & 3 deletions packages/feflow-cli/src/core/commander/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import abbrev from 'abbrev';

export default class Commander {

private store: any;
private invisibleStore: any;
private alias: any;
Expand Down Expand Up @@ -40,5 +39,4 @@ export default class Commander {
this.invisibleStore[name.toLowerCase()] = fn;
this.invisibleStore[name.toLowerCase()].options = options;
}

};
}
23 changes: 15 additions & 8 deletions packages/feflow-cli/src/core/devkit/commandOptions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const getCommandLine = (optionsDescription: any, description: any, cmd: any): Object[] => {
const getCommandLine = (
optionsDescription: any,
description: any,
cmd: any
): Object[] => {
if (Array.isArray(optionsDescription)) return optionsDescription;

const options: Object[] = [];

const optionDescritions = Object.keys(optionsDescription);
if (!optionDescritions.length) return options;

optionDescritions.forEach(option => {
optionDescritions.forEach((option) => {
let optionItemConfig = optionsDescription[option];
const optionDescritionItem = getOptionItem(optionItemConfig, option);
options.push(optionDescritionItem);
Expand All @@ -15,16 +19,16 @@ const getCommandLine = (optionsDescription: any, description: any, cmd: any): Ob
return [
{
header: `fef ${cmd}`,
content: description,
content: description
},
{
header: 'Usage',
content: `$ fef ${cmd} [options]`,
content: `$ fef ${cmd} [options]`
},
{
header: 'Options',
optionList: options,
},
optionList: options
}
];
};

Expand All @@ -33,15 +37,18 @@ const getOptionItem = (optionItemConfig: any, option: any): object => {
if (typeof optionItemConfig == 'string') {
optionDescritionItem = {
name: option,
description: optionItemConfig,
description: optionItemConfig
};
} else {
if (!optionItemConfig.name) {
optionItemConfig.name = option;
}

optionDescritionItem = optionItemConfig;
optionDescritionItem.type = typeof optionItemConfig.type === 'function' ? optionItemConfig.type : String;
optionDescritionItem.type =
typeof optionItemConfig.type === 'function'
? optionItemConfig.type
: String;
}
return optionDescritionItem;
};
Expand Down
40 changes: 18 additions & 22 deletions packages/feflow-cli/src/core/devkit/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ import path from 'path';
import importFresh from 'import-fresh';
import stripComments from 'strip-json-comments';
import yaml from 'js-yaml';
import {
PROJECT_CONFIG,
DEVKIT_CONFIG
} from '../../shared/constant';
import { PROJECT_CONFIG, DEVKIT_CONFIG } from '../../shared/constant';

export default class Config {

public ctx: any;
constructor(ctx: any) {
this.ctx = ctx;
Expand All @@ -25,13 +21,13 @@ export default class Config {
}
}
return false;
}
};

while (!isConfigExits()) {
currDir = path.join(currDir, '../');
if (currDir === '/' || /^[a-zA-Z]:\\$/.test(currDir)) {
return '';
}
currDir = path.join(currDir, '../');
if (currDir === '/' || /^[a-zA-Z]:\\$/.test(currDir)) {
return '';
}
}

return currDir;
Expand All @@ -55,7 +51,7 @@ export default class Config {
try {
configData = this.loadConfigFile(filePath);
} catch (error) {
if (!error || error.code !== "FEFLOW_CONFIG_FIELD_NOT_FOUND") {
if (!error || error.code !== 'FEFLOW_CONFIG_FIELD_NOT_FOUND') {
throw error;
}
}
Expand All @@ -75,17 +71,17 @@ export default class Config {

loadConfigFile(filePath: string) {
switch (path.extname(filePath)) {
case ".js":
case '.js':
return this.loadJSConfigFile(filePath);

case ".json":
if (path.basename(filePath) === "package.json") {
case '.json':
if (path.basename(filePath) === 'package.json') {
return this.loadPackageJSONConfigFile(filePath);
}
return this.loadJSONConfigFile(filePath);

case ".yaml":
case ".yml":
case '.yaml':
case '.yml':
return this.loadYAMLConfigFile(filePath);

default:
Expand Down Expand Up @@ -120,10 +116,10 @@ export default class Config {
try {
const packageData = this.loadJSONConfigFile(filePath);

if (!Object.hasOwnProperty.call(packageData, "feflowConfig")) {
if (!Object.hasOwnProperty.call(packageData, 'feflowConfig')) {
throw Object.assign(
new Error("package.json file doesn't have 'feflowConfig' field."),
{ code: "FEFLOW_CONFIG_FIELD_NOT_FOUND" }
{ code: 'FEFLOW_CONFIG_FIELD_NOT_FOUND' }
);
}

Expand All @@ -143,7 +139,7 @@ export default class Config {
} catch (e) {
this.ctx.logger.debug(`Error reading JSON file: ${filePath}`);
e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
e.messageTemplate = "failed-to-read-json";
e.messageTemplate = 'failed-to-read-json';
e.messageData = {
path: filePath,
message: e.message
Expand All @@ -157,13 +153,13 @@ export default class Config {
try {
return yaml.safeLoad(stripComments(this.readFile(filePath))) || {};
} catch (e) {
this.ctx.logger.debug("Error reading YAML file: %s\n%o", filePath, e);
this.ctx.logger.debug('Error reading YAML file: %s\n%o', filePath, e);
e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
throw e;
}
}

readFile(filePath: string) {
return fs.readFileSync(filePath, "utf8").replace(/^\ufeff/u, "");
return fs.readFileSync(filePath, 'utf8').replace(/^\ufeff/u, '');
}
}
}
56 changes: 40 additions & 16 deletions packages/feflow-cli/src/core/devkit/loadDevkits.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,57 @@
import path from 'path';
import Config from './config';
import getCommandLine from "./commandOptions";
import getCommandLine from './commandOptions';
import { FEFLOW_ROOT } from '../../shared/constant';

const registerDevkitCommand = (command: any, commandConfig: any, directoryPath: any, ctx: any) => {
const registerDevkitCommand = (
command: any,
commandConfig: any,
directoryPath: any,
ctx: any
) => {
const builder = commandConfig.builder;
const [ packageName ] = builder.split(':', 2);
const [packageName] = builder.split(':', 2);
const config = new Config(ctx);
const pkgPath = path.join(directoryPath, 'node_modules', packageName);
try {
const devkitConfig = config.loadDevkitConfig(pkgPath);
const { implementation, description, optionsDescription, usage = {} } = devkitConfig.builders[command];
const {
implementation,
description,
optionsDescription,
usage = {}
} = devkitConfig.builders[command];

const options = getCommandLine(optionsDescription || usage, description, command);
const options = getCommandLine(
optionsDescription || usage,
description,
command
);
if (Array.isArray(implementation)) {
ctx.commander.register(command, description, async () => {
for (let i = 0; i < implementation.length; i ++) {
const action = path.join(pkgPath, implementation[i]);
await require(action)(ctx);
}
}, options);
ctx.commander.register(
command,
description,
async () => {
for (let i = 0; i < implementation.length; i++) {
const action = path.join(pkgPath, implementation[i]);
await require(action)(ctx);
}
},
options
);
} else {
const action = path.join(pkgPath, implementation);
ctx.commander.register(command, description, () => {
require(action)(ctx);
}, options);
ctx.commander.register(
command,
description,
() => {
require(action)(ctx);
},
options
);
}
} catch (e) {
ctx.logger.debug(`${ pkgPath } not found!`);
ctx.logger.debug(`${pkgPath} not found!`);
}
};

Expand All @@ -51,7 +75,7 @@ export default function loadDevkits(ctx: any): Promise<void> {
ctx.logger.debug('Run commands in .fef root will not work.');
} else {
ctx.logger.error(
`A config file .feflowrc(.js|.yaml|.yml|.json) was detected in ${directoryPath}, but lost required property 'commands' in field 'devkit'. Please check your config file or just delete it.`,
`A config file .feflowrc(.js|.yaml|.yml|.json) was detected in ${directoryPath}, but lost required property 'commands' in field 'devkit'. Please check your config file or just delete it.`
);
}
}
Expand Down
Loading

0 comments on commit d1a09d1

Please sign in to comment.