create-qbi-app
Advanced tools
Comparing version 0.0.3 to 0.1.0-beta
@@ -1,47 +0,50 @@ | ||
const fs = require('fs'); | ||
const rimraf = require('rimraf'); | ||
const child_process = require('child_process'); | ||
const logger = require('./utils/logger'); | ||
const { FRAMEWORK_BRANCH, GIT } = require('./utils/config'); | ||
const path = require('path'); | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createApp = void 0; | ||
const fs = require("fs"); | ||
const child_process_1 = require("child_process"); | ||
const logger_1 = require("./utils/logger"); | ||
const config_1 = require("./utils/config"); | ||
const path = require("path"); | ||
const cwd = process.cwd(); | ||
const execSync = child_process.execSync; | ||
/** | ||
* 从 git 仓库拉取模板代码并写入目标目录 | ||
*/ | ||
function createApp(projectName, framework) { | ||
const branch = FRAMEWORK_BRANCH[framework]; | ||
const cmd = `git clone --quiet --depth=1 --branch=${branch} ${GIT} ./${projectName}`; | ||
execSync(cmd); | ||
logger.info(`Creating new ${framework} project "${projectName}" ...`); | ||
logger.info(cmd); | ||
const projectPackage = path.resolve(cwd, `./${projectName}/package.json`); | ||
fs.readFile(projectPackage, 'utf8', function (err, data) { | ||
if (err) { | ||
rimraf.sync(path.resolve(cwd, `./${projectName}`)); | ||
throw new Error(err); | ||
} | ||
rimraf.sync(path.resolve(cwd, `./${projectName}/.git`)); | ||
const json = JSON.parse(data); | ||
json.name = projectName; | ||
json.version = '0.1.0'; | ||
fs.writeFile(projectPackage, JSON.stringify(json, null, 2), function () { | ||
logger.done( | ||
`Create success! Learn more: https://www.yuque.com/u2227425/ia1pn8/xaia8g#XmLfe`, | ||
); | ||
function createApp(projectName, branch) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmd = `git clone --quiet --depth=1 --branch=${branch} ${config_1.GIT} ./${projectName}`; | ||
(0, child_process_1.execSync)(cmd); | ||
logger_1.default.info(`Creating new project "${projectName}" ...`); | ||
logger_1.default.info(cmd); | ||
const projectPackage = path.resolve(cwd, `./${projectName}/package.json`); | ||
fs.readFile(projectPackage, 'utf8', function (err, data) { | ||
if (err) { | ||
fs.rmSync(path.resolve(cwd, `./${projectName}`), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
throw err; | ||
} | ||
fs.rmSync(path.resolve(cwd, `./${projectName}/.git`), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
const json = JSON.parse(data); | ||
json.name = projectName; | ||
json.version = '0.1.0'; | ||
fs.writeFile(projectPackage, JSON.stringify(json, null, 2), function () { | ||
logger_1.default.done(`Create success! Learn more: https://www.yuque.com/u2227425/ia1pn8/xaia8g#XmLfe`); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
module.exports = { | ||
createApp, | ||
}; | ||
exports.createApp = createApp; |
132
bin/index.js
#! /usr/bin/env node | ||
const program = require('commander'); | ||
const { createApp } = require('./create'); | ||
const packageJson = require('../package.json'); | ||
const { | ||
validate, | ||
validateProjectName, | ||
validateUniqueProjectName, | ||
validateFrameworkName, | ||
} = require('./utils/validate.js'); | ||
const logger = require('./utils/logger'); | ||
const { FRAMEWORK_ORIGIN, FRAMEWORK_BRANCH } = require('./utils/config'); | ||
const framework = Object.keys(FRAMEWORK_BRANCH); | ||
const projectNameValidator = validate([ | ||
validateProjectName, | ||
validateUniqueProjectName, | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const commander_1 = require("commander"); | ||
const inquirer = require("inquirer"); | ||
const create_1 = require("./create"); | ||
const packageJson = require("../package.json"); | ||
const validate_1 = require("./utils/validate"); | ||
const logger_1 = require("./utils/logger"); | ||
const config_1 = require("./utils/config"); | ||
const inquirer_1 = require("./utils/inquirer"); | ||
const program = new commander_1.Command(); | ||
inquirer.registerPrompt('list-desc', inquirer_1.DescList); | ||
const pureTemplate = (0, config_1.templateFilter)(config_1.TEMPLATES); | ||
const projectNameValidator = (0, validate_1.validate)([ | ||
validate_1.validateProjectName, | ||
validate_1.validateUniqueProjectName, | ||
]); | ||
const frameworkValidator = validate([validateFrameworkName]); | ||
const legacyVersionValidator = (0, validate_1.validate)([validate_1.validateLegacyVersion]); | ||
program | ||
.version(packageJson.version, '-v, --version', 'output the current version') | ||
.option( | ||
'-f, --framework [framework]', | ||
`choose a framework you want to work in! the value must be within (${framework.join( | ||
' | ', | ||
)})`, | ||
FRAMEWORK_ORIGIN, | ||
); | ||
.version(packageJson.version, '-v, --version', 'Output the current version.') | ||
.option('-t, --template [template]', `Choose a template you want to work in. the value must be within (${pureTemplate | ||
.map(each => `"${each.name}"`) | ||
.join(' | ')}).`) | ||
.option('-l, --legacy [legacy]', `Use template of legacy version. the value must be within (${Object.keys(config_1.LEGACY_VERSION) | ||
.map(each => `"${each}"`) | ||
.join(' | ')}).`); | ||
program.parse(process.argv); | ||
try { | ||
const projectName = program.args[0]; | ||
if ( | ||
projectNameValidator(projectName) && | ||
frameworkValidator(program.framework) | ||
) { | ||
createApp(projectName, program.framework); | ||
} | ||
} catch (err) { | ||
logger.error(err); | ||
process.exit(1); | ||
function run() { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
let projectName = program.args[0]; | ||
if (!projectName) { | ||
const answer = yield inquirer.prompt({ | ||
message: 'Please input project name.', | ||
type: 'input', | ||
name: 'projectName', | ||
}); | ||
projectName = answer.projectName; | ||
} | ||
// must enter a project name | ||
if (projectNameValidator(projectName)) { | ||
let templateBranch; | ||
let templateChoices = config_1.TEMPLATES; | ||
// use legacy version template | ||
if (program.legacy === true) { | ||
const answer = yield inquirer.prompt({ | ||
message: 'Please choose a legacy version.', | ||
type: 'list', | ||
name: 'legacyVersion', | ||
choices: Object.keys(config_1.LEGACY_VERSION), | ||
}); | ||
templateChoices = config_1.LEGACY_VERSION[answer.legacyVersion]; | ||
} | ||
else if (program.legacy && legacyVersionValidator(program.legacy)) { | ||
templateChoices = config_1.LEGACY_VERSION[program.legacy]; | ||
} | ||
const templateNameValidator = (0, validate_1.validate)([ | ||
(0, validate_1.validateTemplate)(templateChoices), | ||
]); | ||
if (!program.template) { | ||
const answer = yield inquirer.prompt({ | ||
message: 'Please choose a template.', | ||
type: 'list-desc', | ||
name: 'template', | ||
choices: templateChoices, | ||
}); | ||
templateBranch = answer.template; | ||
} | ||
else if (program.template && templateNameValidator(program.template)) { | ||
templateBranch = (_a = (0, config_1.templateFilter)(templateChoices).find(each => each.name === program.template)) === null || _a === void 0 ? void 0 : _a.value; | ||
} | ||
yield (0, create_1.createApp)(projectName, templateBranch); | ||
} | ||
} | ||
catch (err) { | ||
logger_1.default.error(err); | ||
console.log(`\nExample: yarn create qbi-app myChart\n\n${program.helpInformation()}`); | ||
process.exit(1); | ||
} | ||
}); | ||
} | ||
if (!program.args.length) { | ||
program.help(); | ||
} | ||
run(); |
@@ -1,19 +0,59 @@ | ||
const FRAMEWORK_ORIGIN = 'origin'; | ||
const FRAMEWORK_REACT = 'react'; | ||
const FRAMEWORK_VUE = 'vue'; | ||
const FRAMEWORK_BRANCH = { | ||
[FRAMEWORK_ORIGIN]: 'template/origin', | ||
[FRAMEWORK_REACT]: 'template/react', | ||
[FRAMEWORK_VUE]: 'template/vue', | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GIT = exports.templateFilter = exports.LEGACY_VERSION = exports.LEGACY_V312_TEMPLATES = exports.TEMPLATES = void 0; | ||
const inquirer_1 = require("inquirer"); | ||
// latest version templates | ||
exports.TEMPLATES = [ | ||
new inquirer_1.Separator('[custom chart]'), | ||
{ | ||
name: 'chart', | ||
value: 'template/origin', | ||
description: 'Custom chart template', | ||
}, | ||
{ | ||
name: 'chart-react', | ||
value: 'template/react', | ||
description: 'Custom chart template using React framework', | ||
}, | ||
{ | ||
name: 'chart-vue', | ||
value: 'template/vue', | ||
description: 'Custom chart template using Vue framework', | ||
}, | ||
new inquirer_1.Separator('[custom menu]'), | ||
{ | ||
name: 'menu-dashboard-card', | ||
value: 'template/menu-dashboard-card', | ||
description: 'Custom menu of dashboard card', | ||
}, | ||
{ | ||
name: 'menu-excel', | ||
value: 'template/menu-excel', | ||
description: 'Custom menu of web excel', | ||
}, | ||
]; | ||
// legacy version v3.12 templates | ||
exports.LEGACY_V312_TEMPLATES = [ | ||
new inquirer_1.Separator('[custom chart]'), | ||
{ | ||
name: 'chart', | ||
value: 'legacy/v3.12/template/origin', | ||
description: '(v3.12) Custom chart template', | ||
}, | ||
{ | ||
name: 'chart-react', | ||
value: 'legacy/v3.12/template/react', | ||
description: '(v3.12) Custom chart template using React framework', | ||
}, | ||
{ | ||
name: 'chart-vue', | ||
value: 'legacy/v3.12/template/vue', | ||
description: '(v3.12) Custom chart template using Vue framework', | ||
}, | ||
]; | ||
exports.LEGACY_VERSION = { | ||
'v3.12': exports.LEGACY_V312_TEMPLATES, | ||
}; | ||
const GIT = 'https://github.com/haili042/create-qbi-app.git'; | ||
module.exports = { | ||
FRAMEWORK_ORIGIN, | ||
FRAMEWORK_REACT, | ||
FRAMEWORK_BRANCH, | ||
FRAMEWORK_VUE, | ||
GIT, | ||
}; | ||
const templateFilter = (list) => list.filter(each => !(each instanceof inquirer_1.Separator)); | ||
exports.templateFilter = templateFilter; | ||
exports.GIT = 'https://github.com/haili042/create-qbi-app.git'; |
@@ -0,43 +1,40 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function getLogText(args) { | ||
return Array.from(args).join(' '); | ||
return Array.from(args).join(' '); | ||
} | ||
class Logger { | ||
/** | ||
* Outputs arguments with the "done" tag / colors | ||
* | ||
* @param {...*} arguments - arguments passed through to console.info | ||
*/ | ||
static done(/* arguments */) { | ||
console.info('\x1B[32m%s\x1B[0m', getLogText(arguments)); | ||
} | ||
/** | ||
* Outputs arguments with the "info" tag / colors | ||
* | ||
* @param {...*} arguments - arguments passed through to console.info | ||
*/ | ||
static info(/* arguments */) { | ||
console.info('\x1B[34m%s\x1B[0m', getLogText(arguments)); | ||
} | ||
/** | ||
* Outputs arguments with the "warn" tag / colors | ||
* | ||
* @param {...*} arguments - arguments passed through to console.warn | ||
*/ | ||
static warn(/* arguments */) { | ||
console.warn('\x1B[33m%s\x1B[0m', getLogText(arguments)); | ||
} | ||
/** | ||
* Outputs arguments with the "error" tag / colors | ||
* | ||
* @param {...*} arguments - arguments passed through to console.error | ||
*/ | ||
static error(/* arguments */) { | ||
console.error('\x1B[31m%s\x1B[0m', getLogText(arguments)); | ||
} | ||
/** | ||
* Outputs arguments with the "done" tag / colors | ||
* | ||
* @param {...*} arguments - arguments passed through to console.info | ||
*/ | ||
static done(...args) { | ||
console.info('\x1B[32m%s\x1B[0m', getLogText(args)); | ||
} | ||
/** | ||
* Outputs arguments with the "info" tag / colors | ||
* | ||
* @param {...*} arguments - arguments passed through to console.info | ||
*/ | ||
static info(...args) { | ||
console.info('\x1B[34m%s\x1B[0m', getLogText(args)); | ||
} | ||
/** | ||
* Outputs arguments with the "warn" tag / colors | ||
* | ||
* @param {...*} arguments - arguments passed through to console.warn | ||
*/ | ||
static warn(...args) { | ||
console.warn('\x1B[33m%s\x1B[0m', getLogText(args)); | ||
} | ||
/** | ||
* Outputs arguments with the "error" tag / colors | ||
* | ||
* @param {...*} arguments - arguments passed through to console.error | ||
*/ | ||
static error(...args) { | ||
console.error('\x1B[31m%s\x1B[0m', getLogText(args)); | ||
} | ||
} | ||
module.exports = Logger; | ||
exports.default = Logger; |
@@ -1,60 +0,67 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateLegacyVersion = exports.validateTemplate = exports.validateUniqueProjectName = exports.validateProjectName = exports.validate = void 0; | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const config_1 = require("./config"); | ||
const cwd = process.cwd(); | ||
const { FRAMEWORK_BRANCH } = require('./config'); | ||
// 校验多个规则 | ||
function validate(validators = []) { | ||
return function (value) { | ||
let errorMsg; | ||
validators.find(validator => { | ||
const msg = validator(value); | ||
if (typeof msg === 'string') { | ||
errorMsg = msg; | ||
function validate(validators) { | ||
return (value) => { | ||
let errorMsg; | ||
(validators || []).find(validator => { | ||
const msg = validator(value); | ||
if (typeof msg === 'string') { | ||
errorMsg = msg; | ||
return true; | ||
} | ||
if (msg === true) { | ||
errorMsg = `${value} is invalid`; | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (errorMsg) { | ||
throw new Error(errorMsg); | ||
} | ||
return true; | ||
} | ||
if (msg === true) { | ||
errorMsg = `${value} is invalid`; | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (errorMsg) { | ||
throw new Error(errorMsg); | ||
} | ||
return true; | ||
}; | ||
}; | ||
} | ||
exports.validate = validate; | ||
// 校验项目名 | ||
function validateProjectName(projectName) { | ||
if (projectName === undefined || projectName === null) { | ||
return 'You must enter a project name'; | ||
} | ||
if (!/^[a-z][a-z0-9_-]+$/.test(projectName)) { | ||
return `The project must start with a letter and can only contain these symbols: a-z, 0-9, -, _`; | ||
} | ||
} | ||
const validateProjectName = projectName => { | ||
if (projectName === undefined || projectName === null) { | ||
return 'You must enter a project name'; | ||
} | ||
if (!/^[a-z][a-z0-9_-]+$/.test(projectName)) { | ||
return `The project name must start with a letter and can only contain these symbols: a-z, 0-9, -, _`; | ||
} | ||
}; | ||
exports.validateProjectName = validateProjectName; | ||
// 校验项目名是否重名 | ||
function validateUniqueProjectName(projectName) { | ||
const pathName = path.resolve(cwd, projectName); | ||
if (fs.existsSync(pathName)) { | ||
return `File or folder "${pathName}" already exists`; | ||
} | ||
} | ||
const validateUniqueProjectName = projectName => { | ||
const pathName = path.resolve(cwd, projectName); | ||
if (fs.existsSync(pathName)) { | ||
return `File or folder "${pathName}" already exists`; | ||
} | ||
}; | ||
exports.validateUniqueProjectName = validateUniqueProjectName; | ||
// 校验框架名 | ||
function validateFrameworkName(framework) { | ||
if (FRAMEWORK_BRANCH[framework] === undefined) { | ||
return `Expect the framework name to be within (${Object.keys(FRAMEWORK_BRANCH).join(' | ')}), but got "${framework}"`; | ||
} | ||
} | ||
module.exports = { | ||
validate, | ||
validateProjectName, | ||
validateUniqueProjectName, | ||
validateFrameworkName, | ||
const validateTemplate = (templates) => (templateName => { | ||
const templateList = (0, config_1.templateFilter)(templates); | ||
const tpl = templateList.find(each => each.name === templateName); | ||
if (tpl === undefined) { | ||
return `Expect the framework name to be within (${templateList | ||
.map(each => `"${each.name}""`) | ||
.join(' | ')}), but got "${templateName}"`; | ||
} | ||
}); | ||
exports.validateTemplate = validateTemplate; | ||
const validateLegacyVersion = version => { | ||
if (config_1.LEGACY_VERSION[version] === undefined) { | ||
return `Expect the legacy version to be within (${Object.keys(config_1.LEGACY_VERSION) | ||
.map(e => `"${e}"`) | ||
.join(' | ')}), but got "${version}"`; | ||
} | ||
}; | ||
exports.validateLegacyVersion = validateLegacyVersion; |
{ | ||
"name": "create-qbi-app", | ||
"version": "0.0.3", | ||
"version": "0.1.0-beta", | ||
"description": "Command line tool for developing component for Quick BI", | ||
@@ -8,2 +8,5 @@ "author": "Quick BI", | ||
"scripts": { | ||
"watch": "tsc -w --sourceMap", | ||
"start": "ts-node -P ./tsconfig.json ./src/index.ts", | ||
"build": "rm -rf bin && rm -rf .cache && tsc", | ||
"prepublishOnly": "yarn version", | ||
@@ -24,4 +27,9 @@ "postpublish": "git push --tag" | ||
"commander": "6.2.0", | ||
"rimraf": "^2.6.1" | ||
"inquirer": "^8.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/inquirer": "^8.1.3", | ||
"ts-node": "^10.4.0", | ||
"typescript": "^4.4.4" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
24980
17
667
3
2
+ Addedinquirer@^8.2.0
+ Addedansi-escapes@4.3.2(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbl@4.1.0(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedchardet@0.7.0(transitive)
+ Addedcli-cursor@3.1.0(transitive)
+ Addedcli-spinners@2.9.2(transitive)
+ Addedcli-width@3.0.0(transitive)
+ Addedclone@1.0.4(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addeddefaults@1.0.4(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedexternal-editor@3.1.0(transitive)
+ Addedfigures@3.2.0(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedinquirer@8.2.6(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedis-interactive@1.0.0(transitive)
+ Addedis-unicode-supported@0.1.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlog-symbols@4.1.0(transitive)
+ Addedmimic-fn@2.1.0(transitive)
+ Addedmute-stream@0.0.8(transitive)
+ Addedonetime@5.1.2(transitive)
+ Addedora@5.4.1(transitive)
+ Addedos-tmpdir@1.0.2(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedrestore-cursor@3.1.0(transitive)
+ Addedrun-async@2.4.1(transitive)
+ Addedrxjs@7.8.1(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedthrough@2.3.8(transitive)
+ Addedtmp@0.0.33(transitive)
+ Addedtslib@2.8.1(transitive)
+ Addedtype-fest@0.21.3(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwcwidth@1.0.1(transitive)
+ Addedwrap-ansi@6.2.0(transitive)
- Removedrimraf@^2.6.1
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedrimraf@2.7.1(transitive)
- Removedwrappy@1.0.2(transitive)