@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
119 lines • 5.45 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const globby_1 = __importDefault(require("@sprucelabs/globby"));
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const spruce_skill_utils_2 = require("@sprucelabs/spruce-skill-utils");
const createTestOptions_schema_1 = __importDefault(require("./../../../.spruce/schemas/spruceCli/v2020_07_22/createTestOptions.schema"));
const AbstractAction_1 = __importDefault(require("../../AbstractAction"));
class CreateAction extends AbstractAction_1.default {
optionsSchema = createTestOptions_schema_1.default;
invocationMessage = 'Creating a test... 🛡';
async execute(options) {
const normalizedOptions = this.validateAndNormalizeOptions(options);
const { testDestinationDir, namePascal, nameCamel, type } = normalizedOptions;
let resolvedDestination = spruce_skill_utils_2.diskUtil.resolvePath(this.cwd, testDestinationDir, type);
let parentTestClass;
const testFeature = this.parent;
const candidates = await testFeature.buildParentClassCandidates();
if (spruce_skill_utils_2.diskUtil.doesDirExist(resolvedDestination)) {
resolvedDestination =
await this.promptForSubDir(resolvedDestination);
}
if (candidates.length > 0) {
parentTestClass =
await this.promptForParentTestClassAndOptionallyInstallDependencies(candidates, parentTestClass, resolvedDestination);
}
this.ui.startLoading('Generating test file...');
const writer = this.Writer('test');
const isTestFixturesInstalled = !!this.Service('pkg').get('devDependencies.@sprucelabs/spruce-test-fixtures');
let doesStaticTestExist = await this.doesStaticTestAlreadyExist(resolvedDestination);
const results = await writer.writeTest(resolvedDestination, {
...normalizedOptions,
type,
nameCamel,
parentTestClass,
isTestFixturesInstalled,
namePascal: namePascal ?? spruce_skill_utils_1.namesUtil.toPascal(nameCamel),
testType: doesStaticTestExist ? 'static' : 'instance',
});
return {
files: results,
hints: ["run `spruce test` in your skill when you're ready!"],
};
}
async doesStaticTestAlreadyExist(resolvedDestination) {
const matches = await (0, globby_1.default)(resolvedDestination + `/**/*.test.ts`);
let doesStaticTestExist = false;
const match = matches[0];
if (match) {
const contents = spruce_skill_utils_2.diskUtil.readFile(matches[0]);
doesStaticTestExist = contents.includes('static');
}
return doesStaticTestExist;
}
async promptForSubDir(resolvedDestination) {
const match = await this.ui.prompt({
type: 'directory',
label: 'Where should I write this test?',
isRequired: true,
defaultValue: {
path: spruce_skill_utils_2.diskUtil.resolvePath(resolvedDestination),
},
});
resolvedDestination = spruce_skill_utils_2.diskUtil.resolvePath(resolvedDestination, match.path);
return resolvedDestination;
}
async promptForParentTestClassAndOptionallyInstallDependencies(candidates, parentTestClass, resolvedDestination) {
const idx = await this.ui.prompt({
type: 'select',
isRequired: true,
label: 'Which abstract test class do you want to extend?',
options: {
choices: [
{ value: '', label: 'AbstractSpruceTest (default)' },
...candidates.map((candidate, idx) => ({
value: `${idx}`,
label: candidate.label,
})),
],
},
});
if (idx !== '' && candidates[+idx]) {
const match = candidates[+idx];
if (match) {
await this.optionallyInstallFeatureBasedOnSelection(match);
parentTestClass = this.buildParentClassFromCandidate(match, resolvedDestination);
}
}
return parentTestClass;
}
async optionallyInstallFeatureBasedOnSelection(match) {
if (match.featureCode) {
const isInstalled = await this.features.isInstalled(match.featureCode);
if (!isInstalled) {
this.ui.startLoading(`Installing ${match.name}...`);
await this.features.install({
features: [{ code: match.featureCode }],
});
this.ui.stopLoading();
}
}
}
buildParentClassFromCandidate(match, resolvedDestination) {
return {
name: match.name,
label: match.label,
isDefaultExport: match.isDefaultExport,
importPath: match.import ??
spruce_skill_utils_2.diskUtil.resolveRelativePath(resolvedDestination,
//@ts-ignore
match.path.replace(path_1.default.extname(match.path), '')),
};
}
}
exports.default = CreateAction;
//# sourceMappingURL=CreateAction.js.map