Skip to content

Commit

Permalink
feat(rocketact): support custom template
Browse files Browse the repository at this point in the history
  • Loading branch information
beanlee committed Jan 13, 2021
1 parent db7c04c commit 0ecb30f
Show file tree
Hide file tree
Showing 3 changed files with 658 additions and 16 deletions.
7 changes: 5 additions & 2 deletions packages/rocketact/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rocketact",
"version": "0.3.2",
"version": "0.3.3",
"description": "Develop React projects with ease",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -42,7 +42,10 @@
"caporal": "^1.1.0",
"fs-extra": "^8.0.0",
"rocketact-dev-utils": "^1.1.3",
"validate-npm-package-name": "^3.0.0"
"validate-npm-package-name": "^3.0.0",
"latest-version": "^5.1.0",
"download": "^8.0.0",
"decompress": "^4.2.1"
},
"engines": {
"node": ">=10"
Expand Down
45 changes: 41 additions & 4 deletions packages/rocketact/src/bin/rocketact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
success,
info,
infoBlock,
errorBlock,
normalBlock,
successBlock,
packageUtil
Expand All @@ -15,6 +16,9 @@ import * as fs from "fs-extra";
import * as path from "path";
import * as os from "os";

const latestVersion = require("latest-version");
const download = require('download');
const decompress = require('decompress');
const validatePackageName = require("validate-npm-package-name"); // tslint:disable-line

import prog from "caporal";
Expand All @@ -29,6 +33,8 @@ prog
.version(projectMainPkg.version)
.command("create", "create a new project")
.argument("<directory>", "directory to create project")
.option('--template <template>', 'custom template\'s name, ex. demo', prog.STRING, )
// @ts-ignore
.action((args, options, logger) => {
projectName = args.directory;

Expand All @@ -37,7 +43,7 @@ prog
process.exit(0);
}

createProject();
createProject(options.template);
});

prog.parse(process.argv);
Expand Down Expand Up @@ -66,7 +72,7 @@ function printValidationResults(results: [string]): void {
}
}

async function createProject() {
async function createProject(template?: string) {
checkProjectName();

const root = path.resolve(projectName as string);
Expand All @@ -77,7 +83,7 @@ async function createProject() {

fs.ensureDirSync(appName);

await copyTemplateFiles(root);
await copyTemplateFiles(root, template);

process.chdir(root);

Expand Down Expand Up @@ -108,10 +114,15 @@ async function createProject() {
* 拷贝模板文件
*
* @param {string} projectDir
* @param {string} template 模板名称
* @returns
*/
async function copyTemplateFiles(projectDir: string) {
async function copyTemplateFiles(projectDir: string, template?: string) {
try {
if(template) {
await copyTemplateFilesFromTemplate(projectDir, template)
return;
}
await fs.copy(
path.resolve(path.join(__dirname, "../../template")),
projectDir,
Expand All @@ -123,6 +134,32 @@ async function copyTemplateFiles(projectDir: string) {
console.error(err);
}
}

/**
* 根据模板名称下载模板,并创建文件夹
*
* @param projectDir
* @param template
*/
async function copyTemplateFilesFromTemplate(projectDir: string, template: string) {
const latestVersionStr = await latestVersion(`rocketact-template-${template}`).catch((e: any) => {
console.log(`${errorBlock(' Error ')} Initial Falled.`);
console.log(e)
process.exit(1);
});

// TODO: 支持其他 registry 地址
await fs.writeFileSync(`${projectDir}/${template}.tgz`,
await download(`https://registry.npmjs.org/rocketact-template-${template}/-/rocketact-template-${template}-${latestVersionStr}.tgz`));

await decompress(`${projectDir}/${template}.tgz`, `${projectDir}`).then(async () =>{
await fs.copySync(`${projectDir}/package/`, projectDir, { overwrite: true });

await fs.removeSync(`${projectDir}/package/`);
await fs.removeSync(`${projectDir}/${template}.tgz`);
});
}

/**
* Mannualy rename .npmignore to .gitignore
*
Expand Down
Loading

0 comments on commit 0ecb30f

Please sign in to comment.