-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninitit.js
89 lines (75 loc) · 2.06 KB
/
uninitit.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
87
88
89
/*
* A self-bundled copy of a customized version of initit,
* due to: https://github.com/c8r/initit/issues/3
*/
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const exec = require('child_process').execSync;
const spawn = require('cross-spawn');
const install = () => {
return new Promise((resolve, reject) => {
const child = spawn('npm', ['install'], {
stdio: 'inherit'
});
child.on('close', code => {
if (code !== 0) {
reject();
return;
}
resolve();
});
});
};
const gitInit = () => {
exec('git --version', { stdio: 'inherit' });
exec('git init', { stdio: 'inherit' });
exec('git add .', { stdio: 'inherit' });
exec('git commit -am "Init"', { stdio: 'inherit' });
return true;
};
const getTar = ({ user, repo, path = '', name, depth }) => {
const url = `https://codeload.github.com/${user}/${repo}/tar.gz/master`;
const cmd = `curl ${url} | tar -xz -C ${name} --strip=${depth} ${repo}-master/${path}`;
exec(cmd, { stdio: 'inherit' });
};
const create = async (opts = {}) => {
if (!opts.name) {
throw new Error('name argument required');
return;
}
if (!opts.template) {
throw new Error('template argument required');
return;
}
const dirname = path.resolve(opts.name);
const name = path.basename(dirname);
const [user, repo, ...paths] = opts.template.split('/');
const depth = paths.length+1;
console.log(`depth (paths.length): ${depth}`);
fs.ensureDirSync(name);
getTar(
Object.assign({}, opts, {
name,
user,
repo,
path: paths.join('/'),
depth
})
);
const templatePkg = require(path.join(dirname, 'package.json'));
const pkg = Object.assign({}, templatePkg, {
name,
version: '1.0.0'
});
fs.writeFileSync(
path.join(dirname, 'package.json'),
JSON.stringify(pkg, null, 2) + os.EOL
);
process.chdir(dirname);
const installed = await install();
const initialized = gitInit();
exec('npm test', { stdio: 'inherit' });
return { name, dirname };
};
module.exports = create;