UNPKG

mili

Version:

Scaffolding with continuous control over the development of the project.

71 lines (70 loc) 2.82 kB
"use strict"; 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.check = exports.isChildPathOf = void 0; const simple_git_1 = require("simple-git"); const fs = require("fs-extra"); const path = require("path"); const reminder = [ 'This command may overwrite some files', "If you're sure to run the command, rerun it with --force.", ].join('\n'); function isEmptyDir(dir) { const files = fs.readdirSync(dir); return !files.length; } function isChildPathOf(parent) { return (child) => { if (child === parent) return false; const parentTokens = parent.split('/').filter(i => i.length); const childTokens = child.split('/').filter(i => i.length); return parentTokens.every((t, i) => childTokens[i] === t); }; } exports.isChildPathOf = isChildPathOf; function isWorkDirClean(dir) { return __awaiter(this, void 0, void 0, function* () { const git = (0, simple_git_1.default)(dir); const stdout = yield git.raw(['ls-files', '--exclude-standard', '--others', '-m']); const files = stdout ? stdout.split('\n') : []; let toplevel = yield git.revparse(['--show-toplevel']); toplevel = toplevel.replace(/\n$/, ''); return !files .map(file => path.join(toplevel, file)) .filter(isChildPathOf(dir)) .length; }); } function check(dir) { return __awaiter(this, void 0, void 0, function* () { if (!(yield fs.pathExists(dir))) throw new Error(`Not Existed Dir: ${dir}`); const git = (0, simple_git_1.default)(dir); const isRepo = yield git.checkIsRepo(); if (isRepo) { if (!(yield isWorkDirClean(dir))) { throw new Error([ 'Git working directory not clean', reminder, ].join('\n')); } } else if (!isEmptyDir(dir)) { throw new Error([ 'The project directory is not empty.', reminder, ].join('\n')); } return true; }); } exports.check = check;