Created
January 13, 2021 20:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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