Skip to content

Instantly share code, notes, and snippets.

@tynes
Last active March 22, 2019 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tynes/c49956656502c06035d113c3cee3a60a to your computer and use it in GitHub Desktop.
Save tynes/c49956656502c06035d113c3cee3a60a to your computer and use it in GitHub Desktop.
#!/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