@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
158 lines • 7.14 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const spruce_templates_1 = require("@sprucelabs/spruce-templates");
const test_utils_1 = require("@sprucelabs/test-utils");
const EsLint9Migrator_1 = __importDefault(require("../../../migration/EsLint9Migrator"));
const AbstractCliTest_1 = __importDefault(require("../../../tests/AbstractCliTest"));
class UpgradingToEslint9Test extends AbstractCliTest_1.default {
static migrator;
static deletedFiles = [];
static async beforeEach() {
await super.beforeEach();
this.deletedFiles = [];
this.migrator = EsLint9Migrator_1.default.Migrator({ cwd: this.cwd });
delete EsLint9Migrator_1.default.Class;
EsLint9Migrator_1.default.disk = {
...spruce_skill_utils_1.diskUtil,
};
EsLint9Migrator_1.default.disk.deleteFile = (file) => {
this.deletedFiles.push(file);
};
}
static async migratorThrowsWhenMissingRequired() {
//@ts-ignore
const err = test_utils_1.assert.doesThrow(() => EsLint9Migrator_1.default.Migrator());
test_utils_1.errorAssert.assertError(err, 'MISSING_PARAMETERS', {
parameters: ['cwd'],
});
}
static async deletesTheDotEsLintIgnoreIfItExists() {
await this.copyLegacyModuleAndMigrate();
test_utils_1.assert.isEqual(this.deletedFiles[0], this.resolvePath('.eslintignore'));
}
static async skipsIfDotEsLintIgnoreDoesNotExist() {
await this.migrate();
test_utils_1.assert.isLength(this.deletedFiles, 0);
}
static async deletesTheDotEsLintConfig() {
await this.copyOverLegacyModule();
await this.migrate();
test_utils_1.assert.isEqual(this.deletedFiles[1], this.resolvePath('.eslintrc.js'));
}
static async overwritesVsCodeSettingsIfExist() {
await this.copyOverLegacyModule();
const settingsPath = this.resolvePath('.vscode/settings.json');
spruce_skill_utils_1.diskUtil.writeFile(settingsPath, 'old settings');
await this.migrate();
const actual = spruce_skill_utils_1.diskUtil.readFile(settingsPath);
const expected = await spruce_templates_1.templates.vsCodeSettings();
test_utils_1.assert.isEqual(actual, expected);
}
static async doesNotWriteTheFileIfItDoesNotExist() {
await this.copyOverLegacyModule();
const settingsPath = this.resolvePath('.vscode/settings.json');
fs_1.default.rmSync(settingsPath, { force: true });
await this.migrate();
test_utils_1.assert.isFalse(spruce_skill_utils_1.diskUtil.doesDirExist(settingsPath), `Settings file should not exist`);
}
static async callsMigratorInMigrateCommand() {
EsLint9Migrator_1.default.Class = MockMigrator;
await this.copyOverLegacyModule();
this.commandFaker.fakeCommand(/yarn/, 0);
this.commandFaker.fakeCommand(/npm/, 0);
await this.Action('node', 'upgrade').execute({});
MockMigrator.assertDidMigrate();
MockMigrator.assertStartupOptions({ cwd: this.cwd });
}
static async doNotCopyEsLintConfigIfAlreadyExists() {
await this.copyOverLegacyModule();
const eslintrcPath = this.resolvePath('eslint.config.mjs');
spruce_skill_utils_1.diskUtil.writeFile(eslintrcPath, 'old eslintrc');
await this.migrate();
const actual = spruce_skill_utils_1.diskUtil.readFile(eslintrcPath);
test_utils_1.assert.isEqual(actual, 'old eslintrc');
}
static async doNotCopyVsCodeSettingsIfLegacyEsLintConfigDoesNotExist() {
EsLint9Migrator_1.default.disk = spruce_skill_utils_1.diskUtil;
await this.copyLegacyModuleAndMigrate();
EsLint9Migrator_1.default.disk.writeFile = () => test_utils_1.assert.fail('Should not write file');
await this.migrate();
this.migrator = EsLint9Migrator_1.default.Migrator({ cwd: this.cwd });
await this.migrate();
}
static async copyLegacyModuleAndMigrate() {
await this.copyOverLegacyModule();
await this.migrate();
}
static async migrate() {
await this.migrator.migrate();
}
static async copyOverLegacyModule() {
const sourceZip = this.resolvePath(__dirname, '../../testDirsAndFiles/eslint-8-module.zip');
const copyCommand = `cp ${sourceZip} ${this.cwd}`;
const commands = this.Service('command');
await commands.execute(copyCommand);
const unzipCommand = `unzip -o eslint-8-module.zip`;
await commands.execute(unzipCommand);
}
}
exports.default = UpgradingToEslint9Test;
__decorate([
(0, test_utils_1.test)()
], UpgradingToEslint9Test, "migratorThrowsWhenMissingRequired", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingToEslint9Test, "deletesTheDotEsLintIgnoreIfItExists", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingToEslint9Test, "skipsIfDotEsLintIgnoreDoesNotExist", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingToEslint9Test, "deletesTheDotEsLintConfig", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingToEslint9Test, "overwritesVsCodeSettingsIfExist", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingToEslint9Test, "doesNotWriteTheFileIfItDoesNotExist", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingToEslint9Test, "callsMigratorInMigrateCommand", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingToEslint9Test, "doNotCopyEsLintConfigIfAlreadyExists", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingToEslint9Test, "doNotCopyVsCodeSettingsIfLegacyEsLintConfigDoesNotExist", null);
class MockMigrator {
static didMigrate = false;
static constructorOptions;
constructor(options) {
MockMigrator.constructorOptions = options;
}
async migrate() {
MockMigrator.didMigrate = true;
}
static assertDidMigrate() {
test_utils_1.assert.isTrue(this.didMigrate, `Migrator did not migrate`);
}
static assertStartupOptions(options) {
test_utils_1.assert.isEqualDeep(this.constructorOptions, options);
}
static reset() {
this.didMigrate = false;
this.constructorOptions = undefined;
}
}
//# sourceMappingURL=UpgradingToEslint9.test.js.map