@atomist/sdm-pack-fingerprints
Version:
an Atomist SDM Extension Pack for fingerprinting code
97 lines • 3.9 kB
JavaScript
;
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const __1 = require("../..");
/**
* Create fingerprints from JSON files
* @param {string} filenames
* @return {ExtractFingerprint}
*/
function createFileFingerprint(...filenames) {
return createFilesFingerprint("json-file", content => JSON.parse(content), ...filenames);
}
exports.createFileFingerprint = createFileFingerprint;
/**
* Create fingerprints from JSON files
* @param type type of the fingerprint
* @param {(content: string) => any} canonicalize
* @param {string} filenames
* @return {ExtractFingerprint}
*/
function createFilesFingerprint(type, canonicalize, ...filenames) {
return (p) => __awaiter(this, void 0, void 0, function* () {
const fps = [];
yield Promise.all(filenames.map((filename) => __awaiter(this, void 0, void 0, function* () {
const file = yield p.getFile(filename);
if (file) {
const content = yield file.getContent();
const canonicalized = canonicalize(content);
fps.push({
type,
name: filename,
abbreviation: `file-${filename}`,
version: "0.0.1",
data: {
content,
filename,
},
sha: __1.sha256(JSON.stringify(canonicalized)),
});
}
})));
return fps;
});
}
exports.createFilesFingerprint = createFilesFingerprint;
exports.applyFileFingerprint = (p, fp) => __awaiter(this, void 0, void 0, function* () {
const file = yield p.getFile(fp.data.filename);
if (file) {
automation_client_1.logger.info("Update content on an existing file");
yield file.setContent(fp.data.content);
return true;
}
else {
automation_client_1.logger.info("Creating new file '%s'", fp.data.filename);
yield p.addFile(fp.data.filename, fp.data.content);
return true;
}
});
exports.JsonFile = {
displayName: "JSON files",
name: "json-file",
extract: createFileFingerprint("tslint.json", "tsconfig.json"),
apply: exports.applyFileFingerprint,
toDisplayableFingerprint: fp => fp.name,
};
/**
* Create a aspect that handles the given files
* @return {Aspect}
*/
function filesAspect(opts, ...files) {
return Object.assign({}, opts, { extract: createFilesFingerprint(opts.type, opts.canonicalize, ...files), apply: exports.applyFileFingerprint });
}
exports.filesAspect = filesAspect;
//# sourceMappingURL=jsonFiles.js.map