UNPKG

2.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require("fs-extra");
4const _ = require("lodash");
5const memFs = require("mem-fs");
6const editor = require("mem-fs-editor");
7const path = require("path");
8const util_1 = require("../util");
9class Creator {
10 constructor(sourceRoot) {
11 const store = memFs.create();
12 this.fs = editor.create(store);
13 this.sourceRoot(sourceRoot || path.join((0, util_1.getRootPath)()));
14 this.init();
15 }
16 init() { }
17 sourceRoot(rootPath) {
18 if (typeof rootPath === 'string') {
19 this._rootPath = path.resolve(rootPath);
20 }
21 if (!fs.existsSync(this._rootPath)) {
22 fs.ensureDirSync(this._rootPath);
23 }
24 return this._rootPath;
25 }
26 templatePath(...args) {
27 let filepath = path.join.apply(path, args);
28 if (!path.isAbsolute(filepath)) {
29 filepath = path.join(this._rootPath, 'templates', filepath);
30 }
31 return filepath;
32 }
33 destinationRoot(rootPath) {
34 if (typeof rootPath === 'string') {
35 this._destinationRoot = path.resolve(rootPath);
36 if (!fs.existsSync(rootPath)) {
37 fs.ensureDirSync(rootPath);
38 }
39 process.chdir(rootPath);
40 }
41 return this._destinationRoot || process.cwd();
42 }
43 destinationPath(...args) {
44 let filepath = path.join.apply(path, args);
45 if (!path.isAbsolute(filepath)) {
46 filepath = path.join(this.destinationRoot(), filepath);
47 }
48 if (filepath.endsWith('package.json.tmpl')) {
49 filepath = filepath.replace('.tmpl', '');
50 }
51 const basename = path.basename(filepath);
52 if (basename.startsWith('_')) {
53 filepath = path.join(path.dirname(filepath), basename.replace(/^_/, '.'));
54 }
55 return filepath;
56 }
57 template(template, source, dest, data, options) {
58 if (typeof dest !== 'string') {
59 options = data;
60 data = dest;
61 dest = source;
62 }
63 const src = this.templatePath(template, source);
64 if (!fs.existsSync(src))
65 return;
66 this.fs.copyTpl(src, this.destinationPath(dest), Object.assign({ _ }, this, data), options);
67 return this;
68 }
69 writeGitKeepFile(dirname) {
70 dirname = path.resolve(dirname);
71 fs.writeFileSync(path.join(dirname, '.gitkeep'), 'Place hold file', 'utf8');
72 }
73 write() { }
74}
75exports.default = Creator;
76//# sourceMappingURL=creator.js.map
\No newline at end of file