Skip to content

feat(init): support pnpm and drop yarn 2 #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 51 additions & 30 deletions lib/console/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const commandExistsSync = require('command-exists').sync;
const ASSET_DIR = join(__dirname, '../../assets');
const GIT_REPO_URL = 'https://github.com/hexojs/hexo-starter.git';

function initConsole(args) {
async function initConsole(args) {
args = Object.assign({ install: true, clone: true }, args);

const baseDir = this.base_dir;
Expand All @@ -20,54 +20,73 @@ function initConsole(args) {

if (existsSync(target) && readdirSync(target).length !== 0) {
log.fatal(`${chalk.magenta(tildify(target))} not empty, please run \`hexo init\` on an empty folder and then copy your files into it`);
return Promise.reject(new Error('target not empty'));
await Promise.reject(new Error('target not empty'));
}

log.info('Cloning hexo-starter', GIT_REPO_URL);

let promise;

if (args.clone) {
promise = spawn('git', ['clone', '--recurse-submodules', '--depth=1', '--quiet', GIT_REPO_URL, target], {
stdio: 'inherit'
});
try {
await spawn('git', ['clone', '--recurse-submodules', '--depth=1', '--quiet', GIT_REPO_URL, target], {
stdio: 'inherit'
});
} catch (err) {
log.warn('git clone failed. Copying data instead');
await copyAsset(target);
}
} else {
promise = copyAsset(target);
await copyAsset(target);
}

return promise.catch(() => {
log.warn('git clone failed. Copying data instead');

return copyAsset(target);
}).then(() => Promise.all([
await Promise.all([
removeGitDir(target),
removeGitModules(target)
])).then(() => {
if (!args.install) return;
]);
if (!args.install) return;

log.info('Install dependencies');
log.info('Install dependencies');

const npmCommand = commandExistsSync('yarn') ? 'yarn' : 'npm';
let npmCommand = 'npm';
if (commandExistsSync('yarn')) {
npmCommand = 'yarn';
} else if (commandExistsSync('pnpm')) {
npmCommand = 'pnpm';
}

try {
if (npmCommand === 'yarn') {
return spawn(npmCommand, ['install', '--production', '--ignore-optional', '--silent'], {
const yarnVer = await spawn(npmCommand, ['--version'], {
cwd: target
});
if (typeof yarnVer === 'string' && yarnVer.startsWith('1')) {
await spawn(npmCommand, ['install', '--production', '--ignore-optional', '--silent'], {
cwd: target,
stdio: 'inherit'
});
} else {
npmCommand = 'npm';
}
} else if (npmCommand === 'pnpm') {
await spawn(npmCommand, ['install', '--prod', '--no-optional', '--silent'], {
cwd: target,
stdio: 'inherit'
});
}

if (npmCommand === 'npm') {
await spawn(npmCommand, ['install', '--only=production', '--optional=false', '--silent'], {
cwd: target,
stdio: 'inherit'
});
}
return spawn(npmCommand, ['install', '--only=production', '--optional=false', '--silent'], {
cwd: target,
stdio: 'inherit'
});
}).then(() => {
log.info('Start blogging with Hexo!');
}).catch(() => {
} catch (err) {
log.warn(`Failed to install dependencies. Please run 'npm install' in "${target}" folder.`);
});
}
}

function copyAsset(target) {
return copyDir(ASSET_DIR, target, { ignoreHidden: false });
async function copyAsset(target) {
await copyDir(ASSET_DIR, target, { ignoreHidden: false });
}

function removeGitDir(target) {
Expand All @@ -83,11 +102,13 @@ function removeGitDir(target) {
}).then(() => readdir(target)).map(path => join(target, path)).filter(path => stat(path).then(stats => stats.isDirectory())).each(removeGitDir);
}

function removeGitModules(target) {
return unlink(join(target, '.gitmodules')).catch(err => {
async function removeGitModules(target) {
try {
await unlink(join(target, '.gitmodules'));
} catch (err) {
if (err && err.code === 'ENOENT') return;
throw err;
});
}
}

module.exports = initConsole;