Skip to content

Commit

Permalink
🐛 fix execCommand bug - v1.0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffy-g committed Jan 4, 2024
1 parent 43078ca commit c82ad8b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [16.x, 18.x]
node-version: [16.x, 18.x, 21.x]
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
Expand Down
2 changes: 1 addition & 1 deletion cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ let detector;
enumerable: false,
},
version: {
value: "v1.0.18",
value: "v1.0.19",
enumerable: true,
},
});
Expand Down
31 changes: 16 additions & 15 deletions cjs/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ function execCommand(options) {
});
return /** @type {R} */ (p);
}
else {
try {
return /** @type {R} */ (execFileSync(command, args /*, execOpt*/));
}
catch (e) {
return /** @type {R} */ (e);
}
try {
return /** @type {R} */ (execFileSync(command, args, { encoding: "utf8" }));
}
catch (e) {
return /** @type {R} */ (e);
}
}
/**
Expand All @@ -79,14 +77,18 @@ function execCommand(options) {
* * If `result` is empty string, Error object, etc., returns "__en_US__"
*
* @todo latest error cache
* @param {string | any} result `string` or `Error` object
* @param {string | Error} result `string` or `Error` object
* @param {(result: string) => string} [processor] If `result` is a `string`, delegate processing
* @todo strict check of `result`
*/
function validate(result, processor) {
if (typeof result === "string" && result.length) {
return processor ? processor(result) : result.trim();
}
/* istanbul ignore next */
if (result instanceof Error) {
console.error(result);
}
return defaultLocale;
}
/**
Expand Down Expand Up @@ -141,11 +143,11 @@ const [getAppleLocale, getAppleLocaleSync] = /** @type {TAppleLocaleFunctions} *
execCommand({
async: true,
command: cmd0, args: args0
}).then(ret => validate(ret)),
}).then(validate),
execCommand({
async: true,
command: cmd1, args: args1
}).then(ret => validate(ret)),
}).then(validate),
]);
return getSupportedLocale(results[0], results[1]);
},
Expand All @@ -167,7 +169,7 @@ const [getUnixLocale, getUnixLocaleSync] = /** @type {(cmd: TLocalCmdToken) => T
*/
async () => pet(parseLocale(await execCommand({
async: true, command: cmd
}).then(ret => validate(ret)))),
}).then(validate))),
/**
* Locale detection for UNIX OS related
*/
Expand All @@ -182,8 +184,7 @@ const parseLCID = (result) => {
const lcidCode = parseInt(result.replace("Locale", ""), 16);
return lcid.from(lcidCode) || defaultLocale;
};
const [getWinLocale, getWinLocaleSync] = /** @type {(a: TLocalCmdToken, b: string[]) => TAsyncSyncPair} */ ((cmd0, args0) => {
const opt = { command: cmd0, args: args0 };
const [getWinLocale, getWinLocaleSync] = /** @type {(a: TLocalCmdToken, b: string[]) => TAsyncSyncPair} */ ((command, args) => {
return [
/**
* Locale detection for windows OS
Expand All @@ -192,11 +193,11 @@ const [getWinLocale, getWinLocaleSync] = /** @type {(a: TLocalCmdToken, b: strin
*
* @async
*/
async () => validate(await execCommand(opt), parseLCID),
async () => validate(await execCommand({ command, args, async: true }), parseLCID),
/**
* Locale detection for windows OS
*/
() => validate(execCommand(opt), parseLCID)
() => validate(execCommand({ command, args }), parseLCID)
];
})("wmic", ["os", "get", "locale"]);
/** @type {[ TGetLocaleFunctions<string>, TGetLocaleFunctions<Promise<string>> ]} */
Expand Down
2 changes: 1 addition & 1 deletion esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ let detector;
enumerable: false,
},
version: {
value: "v1.0.18",
value: "v1.0.19",
enumerable: true,
},
});
Expand Down
31 changes: 16 additions & 15 deletions esm/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ function execCommand(options) {
});
return /** @type {R} */ (p);
}
else {
try {
return /** @type {R} */ (execFileSync(command, args /*, execOpt*/));
}
catch (e) {
return /** @type {R} */ (e);
}
try {
return /** @type {R} */ (execFileSync(command, args, { encoding: "utf8" }));
}
catch (e) {
return /** @type {R} */ (e);
}
}
/**
Expand All @@ -76,14 +74,18 @@ function execCommand(options) {
* * If `result` is empty string, Error object, etc., returns "__en_US__"
*
* @todo latest error cache
* @param {string | any} result `string` or `Error` object
* @param {string | Error} result `string` or `Error` object
* @param {(result: string) => string} [processor] If `result` is a `string`, delegate processing
* @todo strict check of `result`
*/
function validate(result, processor) {
if (typeof result === "string" && result.length) {
return processor ? processor(result) : result.trim();
}
/* istanbul ignore next */
if (result instanceof Error) {
console.error(result);
}
return defaultLocale;
}
/**
Expand Down Expand Up @@ -138,11 +140,11 @@ const [getAppleLocale, getAppleLocaleSync] = /** @type {TAppleLocaleFunctions} *
execCommand({
async: true,
command: cmd0, args: args0
}).then(ret => validate(ret)),
}).then(validate),
execCommand({
async: true,
command: cmd1, args: args1
}).then(ret => validate(ret)),
}).then(validate),
]);
return getSupportedLocale(results[0], results[1]);
},
Expand All @@ -164,7 +166,7 @@ const [getUnixLocale, getUnixLocaleSync] = /** @type {(cmd: TLocalCmdToken) => T
*/
async () => pet(parseLocale(await execCommand({
async: true, command: cmd
}).then(ret => validate(ret)))),
}).then(validate))),
/**
* Locale detection for UNIX OS related
*/
Expand All @@ -179,8 +181,7 @@ const parseLCID = (result) => {
const lcidCode = parseInt(result.replace("Locale", ""), 16);
return lcid.from(lcidCode) || defaultLocale;
};
const [getWinLocale, getWinLocaleSync] = /** @type {(a: TLocalCmdToken, b: string[]) => TAsyncSyncPair} */ ((cmd0, args0) => {
const opt = { command: cmd0, args: args0 };
const [getWinLocale, getWinLocaleSync] = /** @type {(a: TLocalCmdToken, b: string[]) => TAsyncSyncPair} */ ((command, args) => {
return [
/**
* Locale detection for windows OS
Expand All @@ -189,11 +190,11 @@ const [getWinLocale, getWinLocaleSync] = /** @type {(a: TLocalCmdToken, b: strin
*
* @async
*/
async () => validate(await execCommand(opt), parseLCID),
async () => validate(await execCommand({ command, args, async: true }), parseLCID),
/**
* Locale detection for windows OS
*/
() => validate(execCommand(opt), parseLCID)
() => validate(execCommand({ command, args }), parseLCID)
];
})("wmic", ["os", "get", "locale"]);
/** @type {[ TGetLocaleFunctions<string>, TGetLocaleFunctions<Promise<string>> ]} */
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "os-locale-s",
"version": "1.0.18",
"version": "1.0.19",
"description": "Its a light weight version that minimizes the dependency module of `os-locale`",
"private": false,
"main": "./cjs/index.js",
Expand Down

0 comments on commit c82ad8b

Please sign in to comment.