Repository navigation
Expand file tree
/
Copy pathinitit.js
More file actions
113 lines (98 loc) · 2.77 KB
/
Copy pathinitit.js
File metadata and controls
113 lines (98 loc) · 2.77 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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 https = require("https");
const tar = require("tar-fs");
const gunzip = require("gunzip-maybe");
const install = () => {
return new Promise((resolve, reject) => {
console.log("Installing dependencies...");
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, templatepath = "", name }) => {
return new Promise((resolve, reject) => {
console.log("Downloading template...");
const ignorePrefix = "__INITIT_IGNORE__/";
const ignorepath = path.join(name, ignorePrefix);
const extractTar = tar.extract(name, {
map: header => {
const prefix = `${repo}-master/${templatepath}`;
if (header.name.startsWith(prefix)) {
return Object.assign({}, header, {
name: header.name.substr(prefix.length)
});
} else {
return Object.assign({}, header, {
name: ignorePrefix + header.name
});
}
},
ignore: filepath => {
const isInIgnoreFolder = !path
.relative(ignorepath, filepath)
.startsWith("..");
return isInIgnoreFolder;
}
});
https.get(
`https://github.com/proxy/codeload.github.com/${user}/${repo}/tar.gz/master`,
response => response.pipe(gunzip()).pipe(extractTar)
);
extractTar.on("error", reject);
extractTar.on("finish", resolve);
});
};
const create = async (opts = {}) => {
if (!opts.name) {
throw new Error("name argument required");
}
if (!opts.template) {
throw new Error("template argument required");
}
const dirname = path.resolve(opts.name);
const name = path.basename(dirname);
const [user, repo, ...paths] = opts.template.split("/");
fs.ensureDirSync(name);
await getTar(
Object.assign({}, opts, {
name,
user,
repo,
templatepath: paths.join("/")
})
);
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);
await install();
gitInit();
exec("npm test", { stdio: "inherit" });
return { name, dirname };
};
module.exports = create;