mili
Version:
Scaffolding with continuous control over the development of the project.
66 lines (65 loc) • 2.61 kB
JavaScript
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());
});
};
import simpleGit from 'simple-git';
import * as fs from 'fs-extra';
import * as path from '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;
}
export 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);
};
}
function isWorkDirClean(dir) {
return __awaiter(this, void 0, void 0, function* () {
const git = simpleGit(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;
});
}
export 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 = simpleGit(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;
});
}