Skip to content

Commit aec9cee

Browse files
committed
postinstall script updated for Windows folder paths
1 parent 7041577 commit aec9cee

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

postinstall/install.js

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,51 @@ const path = require('path');
66
console.log("INFO: Adding the required localized resource configuration to the config.json file.");
77

88
// Get the current directory
9-
const crntDir = __dirname;
9+
const crntDir = path.resolve(__dirname);
1010
// Split the whole directory path
11-
const nesting = crntDir.split("/");
11+
let nesting = crntDir.split("/");
12+
// Windows split
13+
if (nesting.length <= 1) {
14+
nesting = crntDir.split("\\");
15+
}
1216
// Check if correctly splitted
1317
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.");
18+
// Find the first node_modules folder index
19+
let idx = nesting.indexOf("node_modules");
20+
// Check if index of the folder was found
21+
if (idx !== -1) {
22+
// Slice unnecessary nodes
23+
const nest = nesting.slice(idx);
24+
if (nest && nest.length > 0) {
25+
const paths = nest.map(m => "..");
26+
// Get the path of the projects root location
27+
const rootDir = path.resolve(path.join(__dirname, paths.join('/')));
28+
const fileLoc = `${rootDir}/config/config.json`;
29+
// Check if config.json file exists
30+
if (fs.existsSync(fileLoc)) {
31+
// Get the config file
32+
const config = fs.readFileSync(fileLoc, "utf8");
33+
if (config && typeof config === "string") {
34+
const contents = JSON.parse(config);
35+
if (contents && contents.localizedResources && !contents.localizedResources.ControlStrings) {
36+
contents.localizedResources["ControlStrings"] = "node_modules/@pnp/spfx-controls-react/lib/loc/{locale}.js";
37+
// Update the file
38+
fs.writeFileSync(fileLoc, JSON.stringify(contents, null, 2));
39+
console.log("INFO: Localized resource added.");
40+
} else {
41+
console.warn(`WARNING: it seems something is wrong with the config.json file or the "ControlStrings" reference was already set.`);
42+
}
3443
} else {
35-
console.warn(`WARNING: it seems something is wrong with the config.json file or the "ControlStrings" reference was already set.`);
44+
console.warn("WARNING: the config.json file was not correctly retrieved.");
3645
}
3746
} else {
38-
console.warn("WARNING: the config.json file was not correctly retrieved.");
47+
console.warn("WARNING: the config.json file does not exist.");
3948
}
4049
} else {
41-
console.warn("WARNING: the config.json file does not exist.");
50+
console.warn("WARNING: something is wrong with the installation path.");
4251
}
4352
} else {
44-
console.warn("WARNING: something is wrong with the installation path.");
53+
console.warn("WARNING: something when wrong during with retrieving the project its root location.");
4554
}
4655
} else {
4756
console.warn("WARNING: something is wrong with the installation path.");

0 commit comments

Comments
 (0)