@lenne.tech/cli
Version:
lenne.Tech CLI: lt
166 lines (165 loc) • 6.23 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Typescript = void 0;
const fs = __importStar(require("fs"));
const path_1 = require("path");
/**
* Common helper functions
*/
class Typescript {
/**
* Constructor for integration of toolbox
*/
constructor(toolbox) {
this.toolbox = toolbox;
}
/**
* Create a simple typescript project
*/
create() {
return __awaiter(this, void 0, void 0, function* () {
// Toolbox features
const { filesystem: { cwd, existsAsync }, helper, npm, print: { error, info, spin, success }, system: { run, startTimer, which }, } = this.toolbox;
// Get project name
const name = yield helper.getInput(null, {
name: 'project name',
showError: true,
});
if (!name) {
return;
}
// Check dir
const dir = (0, path_1.join)(cwd(), name);
if (yield existsAsync(dir)) {
error(`Diretory ${dir} exists!`);
}
// Start timer
const timer = startTimer();
// Init
const cloneSpin = spin(`Init project ${name}`);
// Init gts
fs.mkdirSync(dir);
yield run(`cd ${dir} && npm init -y && npx gts init && npm install -D ts-node`);
// Prepare package.json
const { data, path } = yield npm.getPackageJson({
cwd: dir,
showError: true,
});
if (!path) {
return;
}
data.scripts.start = 'npx ts-node src/index.ts';
data.main = 'build/index.js';
if (!(yield npm.setPackageJson(data, { cwd: dir, showError: true }))) {
return;
}
// Overwrite index.ts
const pathOfIndex = (0, path_1.join)(dir, 'src', 'index.ts');
fs.unlinkSync(pathOfIndex);
fs.writeFileSync(pathOfIndex, "// Write your code here\nconsole.log('hello world!');");
// Init git
if (which('git')) {
yield run('git init');
}
cloneSpin.succeed();
// Success info
success(`Project ${name} was created in ${helper.msToMinutesAndSeconds(timer())}.`);
info('');
});
}
/**
* Open stackblitz
*/
stackblitz() {
return __awaiter(this, void 0, void 0, function* () {
const { default: open } = yield Promise.resolve().then(() => __importStar(require('open')));
return open('https://stackblitz.com/fork/typescript');
});
}
/**
* Download and install Web-Maker
*/
webmaker() {
return __awaiter(this, void 0, void 0, function* () {
// Toolbox features
const { filesystem: { cwd, existsAsync }, git, helper, npm, print: { error, spin }, system: { run }, } = this.toolbox;
// Check git
if (!(yield git.gitInstalled())) {
return;
}
// Get project name
const name = yield helper.getInput(null, {
name: 'project name',
showError: true,
});
if (!name) {
return;
}
// Check dir
const dir = (0, path_1.join)(cwd(), name);
if (yield existsAsync(dir)) {
error(`Diretory ${dir} exists!`);
}
// Clone
const repository = 'https://github.com/chinchang/web-maker.git';
const cloneSpin = spin(`Cloning web-maker: ${repository}`);
yield run(`git clone ${repository} ${dir}`);
cloneSpin.succeed();
// Install packages
if (yield npm.install({ cwd: dir, showError: true })) {
return;
}
// Start
yield run(`cd ${dir} && npm start`);
});
}
}
exports.Typescript = Typescript;
/**
* Extend toolbox
*/
exports.default = (toolbox) => {
toolbox.typescript = new Typescript(toolbox);
};