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)); }); };