Skip to content

Instantly share code, notes, and snippets.

@s-KaiNet
Created January 13, 2021 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-KaiNet/caa55846ae06475df3e43c28e1531784 to your computer and use it in GitHub Desktop.
Save s-KaiNet/caa55846ae06475df3e43c28e1531784 to your computer and use it in GitHub Desktop.
Watch JSON formatting files and dynamically inject "$schema" property in a new generated file.
const { watch } = require('gulp');
const path = require('path');
const fs = require('fs');
const defaultColumnSchemaUrl = 'https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json';
const defaultViewSchemaUrl = 'https://developer.microsoft.com/json-schemas/sp/v2/view-formatting.schema.json';
exports.default = function () {
const watcher = watch(['**/*.column{-,.}format.json', '**/*.view{-,.}format.json']);
watcher.on('change', function (filePath) {
const fileName = path.basename(filePath);
const fileBasePath = path.dirname(filePath);
let data = JSON.parse(fs.readFileSync(filePath));
let index;
if (fileName.indexOf('.column') != 0) {
data = {
'$schema': defaultColumnSchemaUrl,
...data
};
index = fileName.indexOf('.column');
} else {
data = {
'$schema': defaultViewSchemaUrl,
...data
};
index = fileName.indexOf('.view');
}
const generatedName = fileName.substring(0, index) + ".json";
fs.writeFileSync(path.join(fileBasePath, generatedName), JSON.stringify(data, null, 2));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment