Last active
March 22, 2019 01:38
-
-
Save tynes/c49956656502c06035d113c3cee3a60a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
/* | |
* write.js | |
* | |
* usage: writes a string to a file | |
* | |
* $ ./write.js <path to file> <contents of file> | |
*/ | |
const fs = require('fs'); | |
const path = require('path'); | |
const assert = require('assert'); | |
const {argv} = process; | |
const args = argv.slice(2); | |
if (args.length === 0) { | |
console.log('Usage:\n./write.js <file to create> <contents of file>'); | |
return; | |
} | |
let [file, string, enc] = args; | |
assert(file, 'pass file to create as first argument'); | |
assert(string, 'pass contents of file as second argument'); | |
if (!enc) | |
enc = 'utf8' | |
file = path.resolve(file); | |
console.log(`writing file ${file}`); | |
fs.writeFileSync(file, string, { encoding: enc }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment