Skip to content

Commit

Permalink
feat: update feat logic
Browse files Browse the repository at this point in the history
  • Loading branch information
oychao committed Dec 14, 2023
1 parent 76f0c0c commit 6d28414
Showing 3 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "source": "https://github.com/oychao/common-boilerplates" }
{"source":"https://gitee.com/oychao/common-boilerplates"}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "boilerplate-creator",
"version": "3.0.3",
"version": "3.0.4",
"main": "index.js",
"author": {
"name": "ouyang-chao",
@@ -46,7 +46,6 @@
"rollup-plugin-json": "^3.1.0"
},
"dependencies": {
"axios": "^0.24.0",
"chalk": "^4.1.2",
"commander": "^8.3.0",
"fs-extra": "^10.0.0",
37 changes: 17 additions & 20 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -7,14 +7,12 @@ import ora from 'ora';
import readline from 'readline-sync';
import jsonfile from 'promise-jsonfile';
import fsex from 'fs-extra';
import axios from 'axios';
import { version as VERSION } from '../package.json';

// init global constants and variables
let isApp;
let templateSource;
const configFilePath = path.resolve(__dirname, '..', 'config.json');
const regexGithubAddr = /^(https:\/\/github.com\/[a-zA-Z\d-]+\/[a-zA-Z\d-]+(\.git)?)$/;
const BPC_NAME = chalk.bold.blue(`boilerplate-creator ${VERSION}`);
const npmTemps = ['npm', 'npm-ts', 'cli', 'cli-ts'];
const legalTemps = npmTemps.concat(['react', 'react-ts', 'vue', 'vue-ts']);
@@ -93,7 +91,7 @@ const done = async function (spinner, projectName, isApp, isAutoInstall, startTi

const initProgram = async function () {
// get template source
templateSource = (await jsonfile.read(configFilePath)).source;
templateSource = (await jsonfile.read(configFilePath)).source || options.config;
// build a program
program
.name(chalk.green('bpc'))
@@ -122,13 +120,14 @@ const initProgram = async function () {
};

const validate = async function () {
const spinner = spinnerEcho(chalk.keyword('gold')(`checking if template ${options.config ? 'source ' : ''}valid`));
const spinner = spinnerEcho(chalk.keyword('gold')(`checking if template source(${templateSource}) valid`));
const targetTemplate =
options.config ||
`${-1 === templateSource.indexOf('.git') ? templateSource : templateSource.slice(0, -4)}/tree/master/templates/${options.template
}`;
try {
const res = await axios.get(targetTemplate);
const res = await fetch(targetTemplate);

if (200 === res.status) {
spinner.text = chalk.magentaBright('valid template source');
spinner.succeed();
@@ -140,11 +139,12 @@ const validate = async function () {
if ('ECONNRESET' === error.code || 'ENOTFOUND' === error.code) {
spinner.text = chalk.red(`network error, unable to access ${templateSource}`);
} else {
spinner.text = options.config
? chalk.red(`invalid template source, please check if ${options.config} is a valid github repository`)
: chalk.red(`invalid template, please check if templates/${options.template} exists on ${templateSource}`);
spinner.text = chalk.red(`invalid template source(${templateSource}) or invalid template(${options.template})`);
spinner.fail();
spinner.text = chalk.red('try update your template source by: bpc -c %new_git_repo%, or use a valid template');
}
spinner.fail();
shell.echo();
return -1;
}
};
@@ -159,14 +159,11 @@ const main = async function () {
shell.echo(BPC_NAME);

if (options.config) {

if (regexGithubAddr.test(options.config) && -1 !== (await validate())) {
shell.echo();
await jsonfile.write(configFilePath, {
source: options.config
});
shell.echo(chalk.green(' template source address updated'));
}
shell.echo();
await jsonfile.write(configFilePath, {
source: options.config
});
shell.echo(chalk.green(` template source address updated: ${options.config}`));
shell.echo();
return;
}
@@ -179,24 +176,24 @@ const main = async function () {
const projectName = options.init;
if (!options.force && fExists(projectName)) {
shell.echo();
if (!confirm(`${chalk.keyword('orange')(projectName)} already exists, overwrite it? [y/N]`)) {
if (!confirm(`${chalk.keyword('orange')(projectName)} already exists, overwrite it ? [y / N]`)) {
shell.exit(1);
}
}

spinner = spinnerEcho(chalk.keyword('gold')('have a cup of tea while initializing project'));
// windows, not working yet
await fsex.remove(projectName);
// await execAsync(`rm -rf ${projectName}`);
// await execAsync(`rm - rf ${ projectName }`);
await execAsync(`mkdir ${projectName}`);
shell.cd(projectName);
await execAsync('git init');
await execAsync(`git remote add origin ${templateSource}`);
await execAsync('git config core.sparseCheckout true');
// only check specific folder
await execAsync(`echo templates/${options.template} >> .git/info/sparse-checkout`);
await execAsync(`echo templates / ${options.template} >> .git / info / sparse - checkout`);
await execAsync('git pull --depth=1 origin master');
await fsex.copy(`templates/${options.template}/`, './');
await fsex.copy(`templates / ${options.template} / `, './');
if (options.auto) {
spinner.text = chalk.magentaBright('project initialized.');
spinner.succeed();

0 comments on commit 6d28414

Please sign in to comment.