Skip to content

Commit 9382822

Browse files
committed
postinstall script added
1 parent 1372999 commit 9382822

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
**Enhancements**
1010

1111
- Allow the `TaxonomyPicker` to also be used in Application Customizer [#77](https://github.com/SharePoint/sp-dev-fx-controls-react/issues/77)
12+
- Add `npm postinstall` script to automatically add the locale config [#78](https://github.com/SharePoint/sp-dev-fx-controls-react/issues/78)
1213

1314
**Fixes**
1415

docs/documentation/docs/about/release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
**Enhancements**
1010

1111
- Allow the `TaxonomyPicker` to also be used in Application Customizer [#77](https://github.com/SharePoint/sp-dev-fx-controls-react/issues/77)
12+
- Add `npm postinstall` script to automatically add the locale config [#78](https://github.com/SharePoint/sp-dev-fx-controls-react/issues/78)
1213

1314
**Fixes**
1415

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"prepublishOnly": "gulp",
1313
"versionUpdater": "gulp versionUpdater",
1414
"karma": "karma start --circle true",
15-
"changelog": "node scripts/sync-changelogs.js"
15+
"changelog": "node scripts/sync-changelogs.js",
16+
"postinstall": "postinstall/install.js"
1617
},
1718
"dependencies": {
1819
"@pnp/common": "^1.0.1",

postinstall/install.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)