-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Update to N-API - Add support for `GetACP` - Add asynchronously APIs
- Loading branch information
1 parent
e00ea54
commit d231387
Showing
17 changed files
with
389 additions
and
213 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"use strict"; | ||
module.exports = { | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": process.env.NYC_CONFIG ? {} : { | ||
"node": 6, | ||
}, | ||
}, | ||
], | ||
], | ||
"plugins": [ | ||
"@babel/plugin-transform-runtime", | ||
], | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,5 +84,7 @@ typings/ | |
|
||
# End of https://www.gitignore.io/api/node | ||
|
||
build | ||
lib | ||
*.tar.gz | ||
build-tmp-napi-*/ | ||
build/ | ||
lib/ |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
"use strict"; | ||
const binary = require("node-pre-gyp"); | ||
const bindingPath = binary.find(require.resolve("../package.json")); | ||
module.exports = require(bindingPath); | ||
const binding = require(bindingPath); | ||
|
||
binding.get = function get (global) { | ||
return Promise.resolve().then(() => ( | ||
binding.getSync(global) | ||
)); | ||
}; | ||
|
||
binding.set = function set (codepage) { | ||
return Promise.resolve().then(() => ( | ||
binding.setSync(codepage) | ||
)); | ||
}; | ||
|
||
module.exports = binding; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,94 @@ | ||
"use strict"; | ||
const childProcess = require("child_process"); | ||
const path = require("path"); | ||
const chcpCom = path.join( | ||
process.env.windir || process.env.SystemRoot || "C:/Windows", | ||
"System32/chcp.com" | ||
); | ||
const stdout = process.stdout; | ||
const key = "-ms-codepage"; | ||
|
||
function getCP () { | ||
if (stdout[key]) { | ||
return stdout[key]; | ||
} | ||
let cp = childProcess.spawnSync(chcpCom, { | ||
stdio: [ | ||
"inherit", | ||
"pipe", | ||
"ignore", | ||
], | ||
env: {}, | ||
encoding: "ascii", | ||
}).stdout; | ||
cp = +/\d+\s*$/.exec(cp)[0]; | ||
stdout[key] = cp; | ||
return cp; | ||
} | ||
|
||
function setCP (codepage) { | ||
if (stdout[key] && (stdout[key] === codepage)) { | ||
return 0; | ||
} | ||
const status = childProcess.spawnSync("chcp", [String(codepage)], { | ||
stdio: [ | ||
"inherit", | ||
"ignore", | ||
"pipe", | ||
const sysDir = path.join(process.env.windir || process.env.SystemRoot || "C:/Windows", "System32"); | ||
const wmicExe = path.join(sysDir, "wbem/WMIC.exe"); | ||
const chcpCom = path.join(sysDir, "chcp.com"); | ||
const spawnOpts = { | ||
stdio: [ | ||
"inherit", | ||
"pipe", | ||
"ignore", | ||
], | ||
env: {}, | ||
encoding: "ascii", | ||
}; | ||
|
||
function spawnAsync (args, callback) { | ||
const child = childProcess.spawn(args.shift(), args, spawnOpts); | ||
const stdout = []; | ||
child.stdout.on("data", stdout.push.bind(stdout)); | ||
child.on("close", code => { | ||
// eslint-disable-next-line standard/no-callback-literal | ||
callback({ | ||
status: code || 0, | ||
stdout: Buffer.concat(stdout).toString(spawnOpts.encoding), | ||
}); | ||
}); | ||
} | ||
|
||
function spawnSync (args, callback) { | ||
callback(childProcess.spawnSync(args.shift(), args, spawnOpts)); | ||
} | ||
|
||
function getHelper (spawn, global, callback) { | ||
spawn( | ||
global | ||
? [wmicExe, "os", "get", "codeset"] | ||
: [chcpCom], | ||
result => { | ||
const cp = +/\d+\s*$/.exec(result.stdout)[0]; | ||
callback(cp); | ||
} | ||
); | ||
} | ||
|
||
function setHelper (spawn, codepage, callback) { | ||
spawn( | ||
[ | ||
chcpCom, | ||
String(codepage), | ||
], | ||
}).status; | ||
if (!status) { | ||
stdout[key] = codepage; | ||
} | ||
return status; | ||
result => { | ||
result = !result.status; | ||
callback(result); | ||
} | ||
); | ||
} | ||
|
||
function cb2sync (fn, args) { | ||
let rst; | ||
fn.apply(this, args.concat(result => { | ||
rst = result; | ||
})); | ||
return rst; | ||
} | ||
|
||
function cb2promise (fn, args) { | ||
return new Promise(resolve => { | ||
fn.apply(this, args.concat(resolve)); | ||
}); | ||
} | ||
|
||
function get (global) { | ||
return cb2promise(getHelper, [spawnAsync, global]); | ||
} | ||
|
||
function getSync (global) { | ||
return cb2sync(getHelper, [spawnSync, global]); | ||
} | ||
|
||
function set (codepage) { | ||
return cb2promise(setHelper, [spawnAsync, codepage]); | ||
} | ||
|
||
function setSync (codepage) { | ||
return cb2sync(setHelper, [spawnSync, codepage]); | ||
} | ||
|
||
module.exports = { | ||
set: setCP, | ||
get: getCP, | ||
get, | ||
set, | ||
getSync, | ||
setSync, | ||
}; |
Oops, something went wrong.