UNPKG

4.15 kBJavaScriptView Raw
1var _asyncToGenerator = require("@babel/runtime/helpers/asyncToGenerator");
2
3const _require = require(`child_process`),
4 execSync = _require.execSync;
5
6const execa = require(`execa`);
7
8const hostedGitInfo = require(`hosted-git-info`);
9
10const fs = require(`fs-extra`);
11
12const sysPath = require(`path`);
13
14const report = require(`./reporter`);
15
16const spawn = cmd => {
17 const _cmd$split = cmd.split(/\s+/),
18 file = _cmd$split[0],
19 args = _cmd$split.slice(1);
20
21 return execa(file, args, {
22 stdio: `inherit`
23 });
24}; // Checks the existence of yarn package
25// We use yarnpkg instead of yarn to avoid conflict with Hadoop yarn
26// Refer to https://github.com/yarnpkg/yarn/issues/673
27//
28// Returns true if yarn exists, false otherwise
29
30
31const shouldUseYarn = () => {
32 try {
33 execSync(`yarnpkg --version`, {
34 stdio: `ignore`
35 });
36 return true;
37 } catch (e) {
38 return false;
39 }
40}; // Executes `npm install` or `yarn install` in rootPath.
41
42
43const install =
44/*#__PURE__*/
45function () {
46 var _ref = _asyncToGenerator(function* (rootPath) {
47 const prevDir = process.cwd();
48 report.info(`Installing packages...`);
49 process.chdir(rootPath);
50
51 try {
52 let cmd = shouldUseYarn() ? spawn(`yarnpkg`) : spawn(`npm install`);
53 yield cmd;
54 } finally {
55 process.chdir(prevDir);
56 }
57 });
58
59 return function install(_x) {
60 return _ref.apply(this, arguments);
61 };
62}();
63
64const ignored = path => !/^\.(git|hg)$/.test(sysPath.basename(path)); // Copy starter from file system.
65
66
67const copy =
68/*#__PURE__*/
69function () {
70 var _ref2 = _asyncToGenerator(function* (starterPath, rootPath) {
71 // Chmod with 755.
72 // 493 = parseInt('755', 8)
73 yield fs.mkdirp(rootPath, {
74 mode: 493
75 });
76
77 if (!fs.existsSync(starterPath)) {
78 throw new Error(`starter ${starterPath} doesn't exist`);
79 }
80
81 if (starterPath === `.`) {
82 throw new Error(`You can't create a starter from the existing directory. If you want to
83 create a new site in the current directory, the trailing dot isn't
84 necessary. If you want to create a new site from a local starter, run
85 something like "gatsby new new-gatsby-site ../my-gatsby-starter"`);
86 }
87
88 report.info(`Creating new site from local starter: ${starterPath}`);
89 report.log(`Copying local starter to ${rootPath} ...`);
90 yield fs.copy(starterPath, rootPath, {
91 filter: ignored
92 });
93 report.success(`Created starter directory layout`);
94 yield install(rootPath);
95 return true;
96 });
97
98 return function copy(_x2, _x3) {
99 return _ref2.apply(this, arguments);
100 };
101}(); // Clones starter from URI.
102
103
104const clone =
105/*#__PURE__*/
106function () {
107 var _ref3 = _asyncToGenerator(function* (hostInfo, rootPath) {
108 let url; // Let people use private repos accessed over SSH.
109
110 if (hostInfo.getDefaultRepresentation() === `sshurl`) {
111 url = hostInfo.ssh({
112 noCommittish: true
113 }); // Otherwise default to normal git syntax.
114 } else {
115 url = hostInfo.https({
116 noCommittish: true,
117 noGitPlus: true
118 });
119 }
120
121 const branch = hostInfo.committish ? `-b ${hostInfo.committish}` : ``;
122 report.info(`Creating new site from git: ${url}`);
123 yield spawn(`git clone ${branch} ${url} ${rootPath} --single-branch`);
124 report.success(`Created starter directory layout`);
125 yield fs.remove(sysPath.join(rootPath, `.git`));
126 yield install(rootPath);
127 });
128
129 return function clone(_x4, _x5) {
130 return _ref3.apply(this, arguments);
131 };
132}();
133
134/**
135 * Main function that clones or copies the starter.
136 */
137module.exports =
138/*#__PURE__*/
139function () {
140 var _ref4 = _asyncToGenerator(function* (starter, options = {}) {
141 const rootPath = options.rootPath || process.cwd();
142
143 if (fs.existsSync(sysPath.join(rootPath, `package.json`))) {
144 report.panic(`Directory ${rootPath} is already an npm project`);
145 return;
146 }
147
148 const hostedInfo = hostedGitInfo.fromUrl(starter);
149 if (hostedInfo) yield clone(hostedInfo, rootPath);else yield copy(starter, rootPath);
150 });
151
152 return function (_x6) {
153 return _ref4.apply(this, arguments);
154 };
155}();
\No newline at end of file