|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +console.log("INFO: Adding the required localized resource configuration to the config.json file."); |
| 7 | + |
| 8 | +// Get the current directory |
| 9 | +const crntDir = __dirname; |
| 10 | +// Split the whole directory path |
| 11 | +const nesting = crntDir.split("/"); |
| 12 | +// Check if correctly splitted |
| 13 | +if (nesting.length > 0) { |
| 14 | + // Find the first node_modules folder location |
| 15 | + const idx = nesting.indexOf("node_modules"); |
| 16 | + // Slice unnecessary nodes |
| 17 | + const nest = nesting.slice(idx); |
| 18 | + if (nest && nest.length > 0) { |
| 19 | + const paths = nest.map(m => ".."); |
| 20 | + // Get the path of the projects root location |
| 21 | + const rootDir = path.resolve(path.join(__dirname, paths.join('/'))); |
| 22 | + const fileLoc = `${rootDir}/config/config.json`; |
| 23 | + // Check if config.json file exists |
| 24 | + if (fs.existsSync(fileLoc)) { |
| 25 | + // Get the config file |
| 26 | + const config = fs.readFileSync(fileLoc, "utf8"); |
| 27 | + if (config && typeof config === "string") { |
| 28 | + const contents = JSON.parse(config); |
| 29 | + if (contents && contents.localizedResources && !contents.localizedResources.ControlStrings) { |
| 30 | + contents.localizedResources["ControlStrings"] = "node_modules/@pnp/spfx-controls-react/lib/loc/{locale}.js"; |
| 31 | + // Update the file |
| 32 | + fs.writeFileSync(fileLoc, JSON.stringify(contents, null, 2)); |
| 33 | + console.log("INFO: Localized resource added."); |
| 34 | + } else { |
| 35 | + console.warn(`WARNING: it seems something is wrong with the config.json file or the "ControlStrings" reference was already set.`); |
| 36 | + } |
| 37 | + } else { |
| 38 | + console.warn("WARNING: the config.json file was not correctly retrieved."); |
| 39 | + } |
| 40 | + } else { |
| 41 | + console.warn("WARNING: the config.json file does not exist."); |
| 42 | + } |
| 43 | + } else { |
| 44 | + console.warn("WARNING: something is wrong with the installation path."); |
| 45 | + } |
| 46 | +} else { |
| 47 | + console.warn("WARNING: something is wrong with the installation path."); |
| 48 | +} |
0 commit comments