1 | var _asyncToGenerator = require("@babel/runtime/helpers/asyncToGenerator");
|
2 |
|
3 | const _require = require(`child_process`),
|
4 | execSync = _require.execSync;
|
5 |
|
6 | const execa = require(`execa`);
|
7 |
|
8 | const hostedGitInfo = require(`hosted-git-info`);
|
9 |
|
10 | const fs = require(`fs-extra`);
|
11 |
|
12 | const sysPath = require(`path`);
|
13 |
|
14 | const report = require(`./reporter`);
|
15 |
|
16 | const 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 | };
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | const shouldUseYarn = () => {
|
32 | try {
|
33 | execSync(`yarnpkg --version`, {
|
34 | stdio: `ignore`
|
35 | });
|
36 | return true;
|
37 | } catch (e) {
|
38 | return false;
|
39 | }
|
40 | };
|
41 |
|
42 |
|
43 | const install =
|
44 |
|
45 | function () {
|
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 |
|
64 | const ignored = path => !/^\.(git|hg)$/.test(sysPath.basename(path));
|
65 |
|
66 |
|
67 | const copy =
|
68 |
|
69 | function () {
|
70 | var _ref2 = _asyncToGenerator(function* (starterPath, rootPath) {
|
71 |
|
72 |
|
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 | }();
|
102 |
|
103 |
|
104 | const clone =
|
105 |
|
106 | function () {
|
107 | var _ref3 = _asyncToGenerator(function* (hostInfo, rootPath) {
|
108 | let url;
|
109 |
|
110 | if (hostInfo.getDefaultRepresentation() === `sshurl`) {
|
111 | url = hostInfo.ssh({
|
112 | noCommittish: true
|
113 | });
|
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 |
|
136 |
|
137 | module.exports =
|
138 |
|
139 | function () {
|
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 |