UNPKG

meblog

Version:

A simple blog engine for personal blogging

110 lines (109 loc) 4.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const fs_1 = tslib_1.__importDefault(require("fs")); const gulplog_1 = tslib_1.__importDefault(require("gulplog")); const gulp_1 = tslib_1.__importDefault(require("gulp")); const ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors")); const GulpUtils_1 = tslib_1.__importDefault(require("../../core/util/GulpUtils")); const DEFAULT_TEMPLATE_NAME = 'meblog'; const getProjectTemplatePath = (template = DEFAULT_TEMPLATE_NAME) => { const projectTemplatePath = path_1.default.join(__dirname, `../../../templates/${template}`); if (!fs_1.default.existsSync(projectTemplatePath)) { throw new Error(`Project template ${ansi_colors_1.default.red(template)} is not existed`); } return projectTemplatePath; }; const getProjectPath = (args) => { const { outdir, name } = args; return path_1.default.resolve(process.cwd(), outdir || name); }; const isInitInCurrentDir = (args) => { return process.cwd() === getProjectPath(args); }; const customizeOnCopying = (destination, args) => { const { name: projectName } = args; const hasPackageJsonFile = fs_1.default.existsSync(path_1.default.join(destination, 'package.json')); return GulpUtils_1.default.through(function (file, enc, cb) { if (file.basename === 'package.json') { if (hasPackageJsonFile) { cb(); return; } else { const packageJson = JSON.parse(file.contents.toString()); packageJson.name = projectName; file.contents = Buffer.from(JSON.stringify(packageJson, null, 2)); } } cb(null, file); }); }; const copyProjectTemplateToTargetPath = (src, destination, args) => { gulp_1.default.src(`${src}/**/*`, { dot: true }) .pipe(customizeOnCopying(destination, args)) .pipe(gulp_1.default.dest(destination)); }; const initProject = (args) => { const { template, name: projectName } = args; gulplog_1.default.info(`Initializing the project with name ${ansi_colors_1.default.blue(projectName)}\ using template ${ansi_colors_1.default.blue(template)}`); const projectTemplatePath = getProjectTemplatePath(template); const projectPath = getProjectPath(args); copyProjectTemplateToTargetPath(projectTemplatePath, projectPath, args); const initCurrentDir = isInitInCurrentDir(args); gulplog_1.default.info(ansi_colors_1.default.green('Project initialization succeed')); const steps = [ `\t${ansi_colors_1.default.green('meblog')} ${ansi_colors_1.default.blue('sample')}: ${ansi_colors_1.default.cyan('To generate sample posts')}\n`, `\t${ansi_colors_1.default.green('meblog')} ${ansi_colors_1.default.blue('serve')}: ${ansi_colors_1.default.cyan('To start development server')}`, ]; if (!initCurrentDir) { steps.unshift(`\t${ansi_colors_1.default.green('cd')} ${ansi_colors_1.default.blue(`${path_1.default.basename(projectPath)}`)}\n`); } gulplog_1.default.info(`\nNext steps: ${steps.join('')} `); }; exports.default = { command: 'init [name]', describe: 'Init the project', builder: (yargs) => { yargs .positional('name', { type: 'string', describe: 'Project name', }) .option('template', { type: 'string', describe: 'Project template name, default: meblog', alias: 't', }) .option('outdir', { type: 'string', describe: 'Customize output directory', alias: 'o', }) .check((args) => { if (!args['name'] && !args['outdir']) { args['outdir'] = '.'; } const initCurrentDir = isInitInCurrentDir(args); if (initCurrentDir) { args['name'] = path_1.default.basename(process.cwd()); } const { name } = args; if (!name) { throw new Error('Project name is required'); } if (name.length < 3) { throw new Error(`Project name "${name}" is too short`); } if (!args['template']) { args['template'] = DEFAULT_TEMPLATE_NAME; } return true; }); }, handler: initProject, };