@salesforce/plugin-release-management
Version:
A plugin for preparing and publishing npm packages
89 lines • 4.16 kB
JavaScript
;
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const os = require("os");
const fs = require("fs/promises");
const kit_1 = require("@salesforce/kit");
const command_1 = require("@salesforce/command");
const core_1 = require("@salesforce/core");
const core_2 = require("@oclif/core");
const collect_1 = require("./collect");
core_1.Messages.importMessagesDirectory(__dirname);
const messages = core_1.Messages.load('@salesforce/plugin-release-management', 'cli.schemas.compare', [
'description',
'examples',
]);
class Compare extends command_1.SfdxCommand {
async run() {
// The "existing schema" is the schema that is stored at the CLI level
const existing = await collect_1.SchemaUtils.getExistingSchemaFiles();
// The "latest schema" is the schema that is found in the node_modules
const latest = await collect_1.SchemaUtils.getLatestSchemaFiles();
// If there are more latest schema than existing schema, that means that new
// schema was added without also being added at the CLI level.
if (latest.length > existing.length) {
const normalized = latest.map((c) => this.normalizeFilename(c));
const missing = normalized.filter((f) => !existing.includes(f));
throw new core_1.SfError(`Missing files: ${missing.join(', ')}`, 'MissingFilesError', [
'This error means that a new schema file was found in an installed plugin. Try running cli:schemas:collect first.',
], 1);
}
const results = {};
for (const file of existing) {
const correspondingFile = latest.find((f) => {
return this.normalizeFilename(f) === file;
});
if (correspondingFile) {
const fileData = await fs.readFile(file, 'utf8');
const fileContents = (0, kit_1.parseJsonMap)(fileData, file);
const correspondingFileData = await fs.readFile(correspondingFile, 'utf8');
const correspondingFileContents = (0, kit_1.parseJsonMap)(correspondingFileData, correspondingFile);
const matches = collect_1.SchemaUtils.deepEqual(fileContents, correspondingFileContents);
results[file] = { correspondingFile, matches };
}
else {
results[file] = {
correspondingFile: null,
matches: false,
reason: 'No corresponding file found in node_modules',
};
}
}
if (!this.flags.json) {
const data = Object.entries(results).reduce((x, [file, d]) => {
return x.concat(Object.assign({ file }, d));
}, []);
const columns = {
file: { header: 'File' },
correspondingFile: { header: 'Corresponding File' },
matches: { header: 'Matches?' },
};
core_2.CliUx.ux.table(data, columns);
}
const hasErrors = Object.values(results).some((result) => result.matches === false);
if (hasErrors) {
throw new core_1.SfError('Found schema changes', 'SchemaMismatchError', [
'This error means that the schema in an installed plugin have changed. If this is intentional, try running cli:schemas:collect first.',
], 1);
}
return results;
}
normalizeFilename(file) {
const normalized = file
.split(path.sep)
.filter((p) => !['node_modules', 'schemas'].includes(p))
.join(path.sep);
return path.join('schemas', normalized);
}
}
exports.default = Compare;
Compare.description = messages.getMessage('description');
Compare.examples = messages.getMessage('examples').split(os.EOL);
Compare.flagsConfig = {};
//# sourceMappingURL=compare.js.map