normalize / mz Goto Github PK
View Code? Open in Web Editor NEWmodernize node.js to current ECMAScript standards
License: MIT License
modernize node.js to current ECMAScript standards
License: MIT License
Is there any reason this isn't included?
http://devdocs.io/iojs/child_process#child_process_child_process_spawn_command_args_options
exec appears to work differently for move operations (linux mv command). I am not sure whether this is a bug or whether mv is a special case. Please take a look at the two code snippets below. The first code block works as expected, but the second code block does not.
The code below works as expected, i.e. the promise resolves after the move operation finishes
const exec = require('child_process').exec;
//inside some function:
return new Promise((resolve, reject) => {
exec('mv file1 file2', (err, out) => {
if (err)
reject(err);
else
resolve(out);
})
});
The code below resolves before mv finishes.
const exec = require('mz/child_process').exec;
//inside some function:
return exec('mv file1 file2')
last commit was 6 years ago
Node now ships with an fs promise API, it would be cool if mz would detect if that API is available (optionally?) and use it if it's there.
It appears fs.truncate is missing from the list of fs modules to wrap -- is this intentional? I can confirm that adding 'truncate'
to the api
array allows me to use it as intended.
Not a huge deal, though, I can just use ftruncate.
Hi,
I love the idea of mz. I mainly use Typescript. Do you known if anyone has started writing a Type Definition for mz ?
I am OK to make one, but I'd rather not redo something existing and currently under work.
Olivier
I was just browsing through the code and came across this:
result.catch(cb).then(function (result) {
process.nextTick(function () { cb(null, result) })
}
So, if result
rejects, we'll .catch(cb)
and call the callback, but then .then(function(result) {...
will be called with whatever cb
returns, so we'll call cb
again next tick. This should be:
result.then(
function (result) {
process.nextTick(function () { cb(null, result) })
},
cb
);
(Edit to add link to code)
fs.mkdtemp(prefix[, options], callback)#
Added in: v5.10.0
why is it a good idea to enable "developers" to continue to use software that is long past is lifespan, and probably a giant security threat?
I was just curious about the decision to use Bluebird Promises in the absence of ES6 Promises. I know this topic has been discussed ad nauseam in other threads, and there are a lot of reasons to think Bluebird Promises are better... but, it does seem like this sets up future pain when switching from Bluebird to native, as I assume many code bases will at some point.
My suggested alternative would be to use a Promise shim that stays spec-compliant.
Currently there is no support for mkdtemp (added in node 5.0.0).
Can you please add a proxy for instances of stream.Writable ?
https://nodejs.org/api/stream.html#stream_class_stream_writable
in particular I would be interested in the instances created by fs.createWriteStream()
https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options
windows10
node v5.10.0
npm v3.9.3
The promise interface does not allow recovering stderr/stdout from child_process.exec
and similar functions, although the callback interface does expose them, in case of error (in particular when the command has a non-zero exit state)
what do you think about :
When I use exec
I am getting different output with mz/child_process
than child_process
. Example program outputting git tags:
const cp = require('child_process');
const mzcp = require('mz/child_process');
function execPromise(cmd) {
return new Promise((resolve, reject) => {
cp.exec(cmd, (err, stdout) => {
err ? reject(err) : resolve(stdout);
});
})
}
async function test(name, exec) {
console.log('TESTING: ', name, '\n');
const stdout = await exec('git tag --sort version:refname');
console.log(stdout.toString('utf8'));
console.log('END TEST: ', name, '\n\n');
}
async function main() {
await test('node', execPromise);
await test('mz', mzcp.exec);
}
main();
child_process
output
TESTING: node
v0.1.0
v0.2.1
v0.2.2
v0.2.3
v0.3.0
v0.4.0
v0.4.1
v0.5.0
v0.6.0
END TEST: node
mz/child_process
output
TESTING: mz
[ 'v0.1.0\nv0.2.1\nv0.2.2\nv0.2.3\nv0.3.0\nv0.4.0\nv0.4.1\nv0.5.0\nv0.6.0\n',
'' ]
END TEST: mz
Running git tag --sort version:refname
in terminal
bash-3.2$ git tag --sort version:refname
v0.1.0
v0.2.1
v0.2.2
v0.2.3
v0.3.0
v0.4.0
v0.4.1
v0.5.0
v0.6.0
Environment
node: v7.8.0
os: 10.12.4
hi! hey! hello!
simple, i can use simple native child process, and stream the stdout, stderr.
but with mz i cannot! :(
There are many ways that all versions of v8 (all JS engines, in fact) deviate from the spec.
By using the es5-shim
and es6-shim
, you'll ensure that all ES5 and ES6 APIs are spec-compliant and consistent.
I would not recommend using bluebird, because it adds additional methods that are not part of the spec, and it also does not support subclassing in a spec-compliant way. (I'd also recommend object.assign
over object-assign
since it's more spec-compliant, if you end up not wanting the es6-shim
)
os
and process
:)
Not every project will want to rely on all of Bluebird, so any-promise allows for any one of the most popular promise polyfills and libraries to be installed.
This is my code:
const fs = require('mz/fs');
const modules = []
client.on("ready", async () => {
console.log("Logged in to discord!");
client.user.setGame("!help")
// start loading modules
const files = await fs.readdir("./modules").catch(e => {
console.error("Module directory could not be found, aborting", argv.debug ? `\n${error}` : "")
process.exit( 1 )
})
for(const file of files) {
// Make one pass and make the file complete
const stat = await fs.stat(`./modules/${file}`).catch(e => {
console.error("Error stating file.", argv.debug ? `\n${e}` : "")
})
if(stat.isDirectory()) {
return
}
debug(`Loaded ./modules/${file}`)
const mod = require(`./modules/${file}`)
mod.init(client)
modules.push(mod)
};
console.log(`Bot started successfully, ${modules.length} modules loaded`)
});
it never gets to even print the last console.log, no matter how long I leave it to run
Hi,
Stack trace is missing even for non fs
related errors when mz/fs
is used in promise chain:
const fs = require('mz/fs');
let a;
fs.readdir('.')
.then(files => a.invalid)
.catch(console.log);
TypeError: Cannot read property 'invalid' of undefined at fs.readdir.then.files (/Users/ozum/Development/l10n-formatjs/sil.js:10:19)
On the other hand Bluebird
or fs-extra-promise
has stacks:
const Promise = require('bluebird')
const fs = Promise.promisifyAll(require('fs-extra'));
// OR const fs = require('fs-extra-promise');
let a;
fs.readdirAsync('.')
.then(files => a.invalid)
.catch(console.log);
TypeError: Cannot read property 'invalid' of undefined
at fs.readdirAsync.then.files (/Users/ozum/Development/l10n-formatjs/sil.js:9:19)
at tryCatcher (/Users/ozum/Development/l10n-formatjs/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/ozum/Development/l10n-formatjs/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/ozum/Development/l10n-formatjs/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/ozum/Development/l10n-formatjs/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/ozum/Development/l10n-formatjs/node_modules/bluebird/js/release/promise.js:693:18)
at Promise._fulfill (/Users/ozum/Development/l10n-formatjs/node_modules/bluebird/js/release/promise.js:638:18)
at /Users/ozum/Development/l10n-formatjs/node_modules/bluebird/js/release/nodeback.js:42:21
at go$readdir$cb (/Users/ozum/Development/l10n-formatjs/node_modules/fs-extra-promise/node_modules/graceful-fs/graceful-fs.js:149:14)
at FSReqWrap.oncomplete (fs.js:114:15)
added by io.js
Hello,
Since Node v10.1.0, this package throws the warning (node:84425) ExperimentalWarning: The fs.promises API is experimental
.
Test case:
var fs = require('mz/fs')
fs.exists(__filename).then(function (exists) {
if (exists) // do something
console.log('it exists') // Works
})
Version 2.0 was documented to use bluebird if installed.
The switch to any-promise
1.0 in 71ad960 changed that and now the builtin Promise
is used. Shouldn't mz
now be at 3.0
?
It sounds cool, no?
var fs = require('modern/fs')
The name is currently taken by an empty project, I'd assume it'd be possible to raise a dispute on it.
since we have node and io.js now, i'm thinking of a library that normalizes both as much as possible. an example is #11. we should add callbacks back and always return promises so people can always use mz
vs. only for promises.
Is there a reason for this omission? Otherwise I think it should be promisified just the same.
Would you consider integrating https://github.com/request/request-promise-native? I can submit a pull request.
I found this library and I think it's brilliant. but I have some issues with a library that is just a function rimraf
const rimraf = require('mz/rimraf')
...
Error: Cannot find module 'mz/rimraf'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.