Skip to content

Commit

Permalink
build-system/common/diff.js
Browse files Browse the repository at this point in the history
  • Loading branch information
alanorozco committed Feb 23, 2021
1 parent a88cd9b commit 5fa6ff8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 53 deletions.
68 changes: 68 additions & 0 deletions build-system/common/diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* 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.
*/
const argv = require('minimist')(process.argv.slice(2));
const tempy = require('tempy');
const {blue, bold, cyan, red} = require('kleur/colors');
const {getStdout} = require('./process');
const {log, logWithoutTimestamp} = require('./logging');
const {writeFile} = require('fs-extra');

/**
* Diffs a file against content that might replace it.
* @param {string} filepath
* @param {string} content
* @return {!Promise<string>}
*/
const diffTentative = (filepath, content) =>
tempy.write.task(content, (temporary) =>
getStdout(`git -c color.ui=always diff -U1 ${filepath} ${temporary}`)
.trim()
.replace(new RegExp(temporary, 'g'), `/${filepath}`)
);

/**
* Diffs a file against new content.
* If `argv.fix` is true, the file is written with the new content, otherwise
* errors out.
* @param {string} callerTask
* @param {string} filepath
* @param {string} tentative
*/
async function writeDiffOrFail(callerTask, filepath, tentative) {
const diff = await diffTentative(filepath, tentative);

if (!diff.length) {
return;
}

logWithoutTimestamp();
logWithoutTimestamp(diff);
logWithoutTimestamp();

if (!argv.fix) {
log(red('ERROR:'), cyan(filepath), 'is missing the changes above.');
log('⤷ To automatically apply them, run', cyan(`gulp ${callerTask} --fix`));
throw new Error(`${filepath} is outdated`);
}

await writeFile(filepath, tentative);
log('Wrote', bold(blue(filepath)));
}

module.exports = {
diffTentative,
writeDiffOrFail,
};
60 changes: 7 additions & 53 deletions build-system/tasks/check-video-interface-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const argv = require('minimist')(process.argv.slice(2));
const globby = require('globby');
const tempy = require('tempy');
const {blue, bold, cyan, red} = require('kleur/colors');
const {getStdout} = require('../common/process');
const {log, logWithoutTimestamp} = require('../common/logging');
const {readFile, writeFile} = require('fs-extra');
const {readFile} = require('fs-extra');
const {writeDiffOrFail} = require('../common/diff');

/** Checks or updates 3rd party video player list on this Markdown file. */
const filepath = 'spec/amp-video-interface.md';
Expand Down Expand Up @@ -68,58 +65,15 @@ const getListRegExp = () =>
'gim'
);

/**
* Diffs a file against content that might replace it.
* @param {string} filepath
* @param {string} content
* @return {!Promise<string>}
*/
const diffTentative = (filepath, content) =>
tempy.write.task(content, (temporary) =>
getStdout(
`git -c color.ui=always diff -U1 ${filepath} ${temporary}`
).replace(new RegExp(temporary, 'g'), `/${filepath}`)
);

/**
* @param {string} filepath
* @param {string} content
* @param {string} output
* @param {boolean} write
*/
async function writeOrFailWhenUnequal(
filepath,
content,
output,
write = false
) {
if (output === content) {
return;
}

logWithoutTimestamp();
logWithoutTimestamp(await diffTentative(filepath, output));

if (!write) {
log(red('ERROR:'), cyan(filepath), 'is missing the changes above.');
log(
'⤷ To automatically apply them, run',
cyan('gulp check-video-interface-list --fix')
);
throw new Error(`${filepath} is outdated`);
}

await writeFile(filepath, output);
log('Wrote', bold(blue(filepath)));
}

/**
* Checks or updates 3rd party video player list.
*/
async function checkVideoInterfaceList() {
const content = await readFile(filepath, 'utf-8');
const output = content.replace(getListRegExp(), generateList());
await writeOrFailWhenUnequal(filepath, content, output, argv.fix);
const current = await readFile(filepath, 'utf-8');
const tentative = current.replace(getListRegExp(), generateList());
if (current !== tentative) {
await writeDiffOrFail('check-video-interface-list', filepath, tentative);
}
}

module.exports = {
Expand Down

0 comments on commit 5fa6ff8

Please sign in to comment.