UNPKG

@tarojs/cli

Version:
76 lines 2.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const helper_1 = require("@tarojs/helper"); const _ = require("lodash"); const memFs = require("mem-fs"); const editor = require("mem-fs-editor"); const path = require("path"); const util_1 = require("../util"); class Creator { constructor(sourceRoot) { const store = memFs.create(); this.fs = editor.create(store); this.sourceRoot(sourceRoot || path.join((0, util_1.getRootPath)())); this.init(); } init() { } sourceRoot(rootPath) { if (typeof rootPath === 'string') { this._rootPath = path.resolve(rootPath); } if (!helper_1.fs.existsSync(this._rootPath)) { helper_1.fs.ensureDirSync(this._rootPath); } return this._rootPath; } templatePath(...args) { let filepath = path.join.apply(path, args); if (!path.isAbsolute(filepath)) { filepath = path.join(this._rootPath, 'templates', filepath); } return filepath; } destinationRoot(rootPath) { if (typeof rootPath === 'string') { this._destinationRoot = path.resolve(rootPath); if (!helper_1.fs.existsSync(rootPath)) { helper_1.fs.ensureDirSync(rootPath); } process.chdir(rootPath); } return this._destinationRoot || process.cwd(); } destinationPath(...args) { let filepath = path.join.apply(path, args); if (!path.isAbsolute(filepath)) { filepath = path.join(this.destinationRoot(), filepath); } if (filepath.endsWith('.tmpl')) { filepath = filepath.replace('.tmpl', ''); } const basename = path.basename(filepath); if (basename.startsWith('_')) { filepath = path.join(path.dirname(filepath), basename.replace(/^_/, '.')); } return filepath; } template(template, source, dest, data, options) { if (typeof dest !== 'string') { options = data; data = dest; dest = source; } const src = this.templatePath(template, source); if (!helper_1.fs.existsSync(src)) return; this.fs.copyTpl(src, this.destinationPath(dest), Object.assign({ _ }, this, data), options); return this; } writeGitKeepFile(dirname) { dirname = path.resolve(dirname); helper_1.fs.writeFileSync(path.join(dirname, '.gitkeep'), 'Place hold file', 'utf8'); } write() { } } exports.default = Creator; //# sourceMappingURL=creator.js.map