/
fetch-npm-starter.js
86 lines (79 loc) 路 2.87 KB
/
fetch-npm-starter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
const path = require('path');
const execa = require('execa');
const chalk = require('chalk');
const stopProcess = require('./stop-process');
/**
* Gets the package version on npm. Will fail if the package does not exist
* @param {string} packageName Name to look up on npm, may include a specific version
* @param {Object} options
* @param {boolean} options.useYarn Yarn instead of npm
* @returns {Object}
*/
async function getPackageInfo(packageName, { useYarn } = {}) {
// Use yarn if possible because it's faster
if (useYarn) {
const { stdout } = await execa('yarn', ['info', packageName, '--json']);
const yarnInfo = JSON.parse(stdout);
return {
name: yarnInfo.data.name,
version: yarnInfo.data.version,
};
}
// Fallback to npm
const { stdout } = await execa('npm', ['view', packageName, 'name', 'version', '--silent']);
// Use regex to parse name and version from CLI result
const [name, version] = stdout.match(/(?<=')(.*?)(?=')/gm);
return { name, version };
}
/**
* Get the version and full package name of the starter
* @param {string} starter - The name of the starter as provided by the user
* @param {Object} options
* @param {boolean} options.useYarn - Use yarn instead of npm
* @returns {Object} - Full name and version of the starter package on npm
*/
async function getStarterPackageInfo(starter, { useYarn } = {}) {
// Check if starter is a shorthand
try {
const longhand = `@strapi/starter-${starter}`;
const packageInfo = await getPackageInfo(longhand, { useYarn });
// Hasn't crashed so it is indeed a shorthand
return packageInfo;
} catch (error) {
// Ignore error, we now know it's not a shorthand
}
// Fetch version of the non-shorthand package
try {
return getPackageInfo(starter, { useYarn });
} catch (error) {
stopProcess(`Could not find package ${chalk.yellow(starter)} on npm`);
}
}
/**
* Download a starter package from the npm registry
* @param {Object} packageInfo - Starter's npm package information
* @param {string} packageInfo.name
* @param {string} packageInfo.version
* @param {string} parentDir - Path inside of which we install the starter
* @param {Object} options
* @param {boolean} options.useYarn - Use yarn instead of npm
*/
async function downloadNpmStarter({ name, version }, parentDir, { useYarn } = {}) {
// Download from npm, using yarn if possible
if (useYarn) {
await execa('yarn', ['add', `${name}@${version}`, '--no-lockfile', '--silent'], {
cwd: parentDir,
});
} else {
await execa('npm', ['install', `${name}@${version}`, '--no-save', '--silent'], {
cwd: parentDir,
});
}
// Return the path of the actual starter
const exactStarterPath = path.dirname(
require.resolve(`${name}/package.json`, { paths: [parentDir] })
);
return exactStarterPath;
}
module.exports = { getStarterPackageInfo, downloadNpmStarter };