1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs = require("fs-extra");
|
4 | const _ = require("lodash");
|
5 | const memFs = require("mem-fs");
|
6 | const editor = require("mem-fs-editor");
|
7 | const path = require("path");
|
8 | const util_1 = require("../util");
|
9 | class 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 | }
|
75 | exports.default = Creator;
|
76 |
|
\ | No newline at end of file |