UNPKG

standard-commit

Version:
81 lines (80 loc) 2.83 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.suggestScopes = exports.getPackageSuggestions = exports.getStagedScopesSuggestions = exports.sortScopes = void 0; const gitutils_1 = require("./gitutils"); const path_1 = __importDefault(require("path")); const util_1 = __importDefault(require("util")); const fs_1 = __importDefault(require("fs")); const readFile = util_1.default.promisify(fs_1.default.readFile); function sortScopes(suggestions) { return Object.keys(suggestions).sort((a, b) => { return suggestions[b] - suggestions[a]; }); } exports.sortScopes = sortScopes; async function getStagedScopesSuggestions() { const paths = await (0, gitutils_1.gitStagedPaths)(); const suggestions = sortScopes(paths.reduce((s, f) => { const { name, dir } = path_1.default.parse(f); const keys = ['*', ...dir.split('/').filter((d) => d), name].reverse(); let weight = 1; for (const k of keys) { s[k] = weight++; } return s; }, {})); return suggestions; } exports.getStagedScopesSuggestions = getStagedScopesSuggestions; async function getPackageSuggestions(config) { const topLevel = (0, gitutils_1.gitTopLevel)(); const unstagedPaths = await (0, gitutils_1.gitStagedPaths)(); const paths = sortScopes(unstagedPaths.reduce((s, f) => { const packages = []; f = path_1.default.dirname(f); while (f != '.') { packages.push(path_1.default.join(f, 'package.json')); f = path_1.default.dirname(f); } for (const pkg of packages) { const weight = s[pkg]; s[pkg] = weight ? weight + 1 : 1; } return s; }, {})); paths.push('package.json'); const rootDir = await topLevel; const suggestions = []; for (let f of paths) { try { f = path_1.default.join(rootDir, f); const pck = await readFile(f, 'utf8'); let { name } = JSON.parse(pck); if (config.stripPackageScope) { name = name.replace(/@.+\//, ''); } suggestions.push(name); } catch { } } return suggestions; } exports.getPackageSuggestions = getPackageSuggestions; async function suggestScopes(config) { if (config.promptScope) { if (config.scopes == 'staged') { return getStagedScopesSuggestions(); } else if (config.scopes == 'packages') { return getPackageSuggestions(config); } else { return config.scopes || []; } } return []; } exports.suggestScopes = suggestScopes;