UNPKG

@buurman/buurman-official-task

Version:

The buurman task to generate official buurman tasks

1,695 lines (1,465 loc) 97 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var sdk = require('@buurman/sdk'); var execa = _interopDefault(require('execa')); var fs = _interopDefault(require('fs')); var constants = _interopDefault(require('constants')); var stream = _interopDefault(require('stream')); var util = _interopDefault(require('util')); var assert = _interopDefault(require('assert')); var path = require('path'); var path__default = _interopDefault(path); var os = _interopDefault(require('os')); var template = _interopDefault(require('lodash.template')); var prettier = require('prettier'); var recursive = _interopDefault(require('recursive-readdir')); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __awaiter(thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } function __values(o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var fromCallback = function (fn) { return Object.defineProperty(function () { if (typeof arguments[arguments.length - 1] === 'function') fn.apply(this, arguments); else { return new Promise((resolve, reject) => { arguments[arguments.length] = (err, res) => { if (err) return reject(err) resolve(res); }; arguments.length++; fn.apply(this, arguments); }) } }, 'name', { value: fn.name }) }; var fromPromise = function (fn) { return Object.defineProperty(function () { const cb = arguments[arguments.length - 1]; if (typeof cb !== 'function') return fn.apply(this, arguments) else fn.apply(this, arguments).then(r => cb(null, r), cb); }, 'name', { value: fn.name }) }; var universalify = { fromCallback: fromCallback, fromPromise: fromPromise }; var origCwd = process.cwd; var cwd = null; var platform = process.env.GRACEFUL_FS_PLATFORM || process.platform; process.cwd = function() { if (!cwd) cwd = origCwd.call(process); return cwd }; try { process.cwd(); } catch (er) {} var chdir = process.chdir; process.chdir = function(d) { cwd = null; chdir.call(process, d); }; var polyfills = patch; function patch (fs) { // (re-)implement some things that are known busted or missing. // lchmod, broken prior to 0.6.2 // back-port the fix here. if (constants.hasOwnProperty('O_SYMLINK') && process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) { patchLchmod(fs); } // lutimes implementation, or no-op if (!fs.lutimes) { patchLutimes(fs); } // https://github.com/isaacs/node-graceful-fs/issues/4 // Chown should not fail on einval or eperm if non-root. // It should not fail on enosys ever, as this just indicates // that a fs doesn't support the intended operation. fs.chown = chownFix(fs.chown); fs.fchown = chownFix(fs.fchown); fs.lchown = chownFix(fs.lchown); fs.chmod = chmodFix(fs.chmod); fs.fchmod = chmodFix(fs.fchmod); fs.lchmod = chmodFix(fs.lchmod); fs.chownSync = chownFixSync(fs.chownSync); fs.fchownSync = chownFixSync(fs.fchownSync); fs.lchownSync = chownFixSync(fs.lchownSync); fs.chmodSync = chmodFixSync(fs.chmodSync); fs.fchmodSync = chmodFixSync(fs.fchmodSync); fs.lchmodSync = chmodFixSync(fs.lchmodSync); fs.stat = statFix(fs.stat); fs.fstat = statFix(fs.fstat); fs.lstat = statFix(fs.lstat); fs.statSync = statFixSync(fs.statSync); fs.fstatSync = statFixSync(fs.fstatSync); fs.lstatSync = statFixSync(fs.lstatSync); // if lchmod/lchown do not exist, then make them no-ops if (!fs.lchmod) { fs.lchmod = function (path, mode, cb) { if (cb) process.nextTick(cb); }; fs.lchmodSync = function () {}; } if (!fs.lchown) { fs.lchown = function (path, uid, gid, cb) { if (cb) process.nextTick(cb); }; fs.lchownSync = function () {}; } // on Windows, A/V software can lock the directory, causing this // to fail with an EACCES or EPERM if the directory contains newly // created files. Try again on failure, for up to 60 seconds. // Set the timeout this long because some Windows Anti-Virus, such as Parity // bit9, may lock files for up to a minute, causing npm package install // failures. Also, take care to yield the scheduler. Windows scheduling gives // CPU to a busy looping process, which can cause the program causing the lock // contention to be starved of CPU by node, so the contention doesn't resolve. if (platform === "win32") { fs.rename = (function (fs$rename) { return function (from, to, cb) { var start = Date.now(); var backoff = 0; fs$rename(from, to, function CB (er) { if (er && (er.code === "EACCES" || er.code === "EPERM") && Date.now() - start < 60000) { setTimeout(function() { fs.stat(to, function (stater, st) { if (stater && stater.code === "ENOENT") fs$rename(from, to, CB); else cb(er); }); }, backoff); if (backoff < 100) backoff += 10; return; } if (cb) cb(er); }); }})(fs.rename); } // if read() returns EAGAIN, then just try it again. fs.read = (function (fs$read) { function read (fd, buffer, offset, length, position, callback_) { var callback; if (callback_ && typeof callback_ === 'function') { var eagCounter = 0; callback = function (er, _, __) { if (er && er.code === 'EAGAIN' && eagCounter < 10) { eagCounter ++; return fs$read.call(fs, fd, buffer, offset, length, position, callback) } callback_.apply(this, arguments); }; } return fs$read.call(fs, fd, buffer, offset, length, position, callback) } // This ensures `util.promisify` works as it does for native `fs.read`. read.__proto__ = fs$read; return read })(fs.read); fs.readSync = (function (fs$readSync) { return function (fd, buffer, offset, length, position) { var eagCounter = 0; while (true) { try { return fs$readSync.call(fs, fd, buffer, offset, length, position) } catch (er) { if (er.code === 'EAGAIN' && eagCounter < 10) { eagCounter ++; continue } throw er } } }})(fs.readSync); function patchLchmod (fs) { fs.lchmod = function (path, mode, callback) { fs.open( path , constants.O_WRONLY | constants.O_SYMLINK , mode , function (err, fd) { if (err) { if (callback) callback(err); return } // prefer to return the chmod error, if one occurs, // but still try to close, and report closing errors if they occur. fs.fchmod(fd, mode, function (err) { fs.close(fd, function(err2) { if (callback) callback(err || err2); }); }); }); }; fs.lchmodSync = function (path, mode) { var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode); // prefer to return the chmod error, if one occurs, // but still try to close, and report closing errors if they occur. var threw = true; var ret; try { ret = fs.fchmodSync(fd, mode); threw = false; } finally { if (threw) { try { fs.closeSync(fd); } catch (er) {} } else { fs.closeSync(fd); } } return ret }; } function patchLutimes (fs) { if (constants.hasOwnProperty("O_SYMLINK")) { fs.lutimes = function (path, at, mt, cb) { fs.open(path, constants.O_SYMLINK, function (er, fd) { if (er) { if (cb) cb(er); return } fs.futimes(fd, at, mt, function (er) { fs.close(fd, function (er2) { if (cb) cb(er || er2); }); }); }); }; fs.lutimesSync = function (path, at, mt) { var fd = fs.openSync(path, constants.O_SYMLINK); var ret; var threw = true; try { ret = fs.futimesSync(fd, at, mt); threw = false; } finally { if (threw) { try { fs.closeSync(fd); } catch (er) {} } else { fs.closeSync(fd); } } return ret }; } else { fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb); }; fs.lutimesSync = function () {}; } } function chmodFix (orig) { if (!orig) return orig return function (target, mode, cb) { return orig.call(fs, target, mode, function (er) { if (chownErOk(er)) er = null; if (cb) cb.apply(this, arguments); }) } } function chmodFixSync (orig) { if (!orig) return orig return function (target, mode) { try { return orig.call(fs, target, mode) } catch (er) { if (!chownErOk(er)) throw er } } } function chownFix (orig) { if (!orig) return orig return function (target, uid, gid, cb) { return orig.call(fs, target, uid, gid, function (er) { if (chownErOk(er)) er = null; if (cb) cb.apply(this, arguments); }) } } function chownFixSync (orig) { if (!orig) return orig return function (target, uid, gid) { try { return orig.call(fs, target, uid, gid) } catch (er) { if (!chownErOk(er)) throw er } } } function statFix (orig) { if (!orig) return orig // Older versions of Node erroneously returned signed integers for // uid + gid. return function (target, options, cb) { if (typeof options === 'function') { cb = options; options = null; } function callback (er, stats) { if (stats) { if (stats.uid < 0) stats.uid += 0x100000000; if (stats.gid < 0) stats.gid += 0x100000000; } if (cb) cb.apply(this, arguments); } return options ? orig.call(fs, target, options, callback) : orig.call(fs, target, callback) } } function statFixSync (orig) { if (!orig) return orig // Older versions of Node erroneously returned signed integers for // uid + gid. return function (target, options) { var stats = options ? orig.call(fs, target, options) : orig.call(fs, target); if (stats.uid < 0) stats.uid += 0x100000000; if (stats.gid < 0) stats.gid += 0x100000000; return stats; } } // ENOSYS means that the fs doesn't support the op. Just ignore // that, because it doesn't matter. // // if there's no getuid, or if getuid() is something other // than 0, and the error is EINVAL or EPERM, then just ignore // it. // // This specific case is a silent failure in cp, install, tar, // and most other unix tools that manage permissions. // // When running as root, or if other types of errors are // encountered, then it's strict. function chownErOk (er) { if (!er) return true if (er.code === "ENOSYS") return true var nonroot = !process.getuid || process.getuid() !== 0; if (nonroot) { if (er.code === "EINVAL" || er.code === "EPERM") return true } return false } } var Stream = stream.Stream; var legacyStreams = legacy; function legacy (fs) { return { ReadStream: ReadStream, WriteStream: WriteStream } function ReadStream (path, options) { if (!(this instanceof ReadStream)) return new ReadStream(path, options); Stream.call(this); var self = this; this.path = path; this.fd = null; this.readable = true; this.paused = false; this.flags = 'r'; this.mode = 438; /*=0666*/ this.bufferSize = 64 * 1024; options = options || {}; // Mixin options into this var keys = Object.keys(options); for (var index = 0, length = keys.length; index < length; index++) { var key = keys[index]; this[key] = options[key]; } if (this.encoding) this.setEncoding(this.encoding); if (this.start !== undefined) { if ('number' !== typeof this.start) { throw TypeError('start must be a Number'); } if (this.end === undefined) { this.end = Infinity; } else if ('number' !== typeof this.end) { throw TypeError('end must be a Number'); } if (this.start > this.end) { throw new Error('start must be <= end'); } this.pos = this.start; } if (this.fd !== null) { process.nextTick(function() { self._read(); }); return; } fs.open(this.path, this.flags, this.mode, function (err, fd) { if (err) { self.emit('error', err); self.readable = false; return; } self.fd = fd; self.emit('open', fd); self._read(); }); } function WriteStream (path, options) { if (!(this instanceof WriteStream)) return new WriteStream(path, options); Stream.call(this); this.path = path; this.fd = null; this.writable = true; this.flags = 'w'; this.encoding = 'binary'; this.mode = 438; /*=0666*/ this.bytesWritten = 0; options = options || {}; // Mixin options into this var keys = Object.keys(options); for (var index = 0, length = keys.length; index < length; index++) { var key = keys[index]; this[key] = options[key]; } if (this.start !== undefined) { if ('number' !== typeof this.start) { throw TypeError('start must be a Number'); } if (this.start < 0) { throw new Error('start must be >= zero'); } this.pos = this.start; } this.busy = false; this._queue = []; if (this.fd === null) { this._open = fs.open; this._queue.push([this._open, this.path, this.flags, this.mode, undefined]); this.flush(); } } } var clone_1 = clone; function clone (obj) { if (obj === null || typeof obj !== 'object') return obj if (obj instanceof Object) var copy = { __proto__: obj.__proto__ }; else var copy = Object.create(null); Object.getOwnPropertyNames(obj).forEach(function (key) { Object.defineProperty(copy, key, Object.getOwnPropertyDescriptor(obj, key)); }); return copy } var gracefulFs = createCommonjsModule(function (module) { /* istanbul ignore next - node 0.x polyfill */ var gracefulQueue; var previousSymbol; /* istanbul ignore else - node 0.x polyfill */ if (typeof Symbol === 'function' && typeof Symbol.for === 'function') { gracefulQueue = Symbol.for('graceful-fs.queue'); // This is used in testing by future versions previousSymbol = Symbol.for('graceful-fs.previous'); } else { gracefulQueue = '___graceful-fs.queue'; previousSymbol = '___graceful-fs.previous'; } function noop () {} var debug = noop; if (util.debuglog) debug = util.debuglog('gfs4'); else if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || '')) debug = function() { var m = util.format.apply(util, arguments); m = 'GFS4: ' + m.split(/\n/).join('\nGFS4: '); console.error(m); }; // Once time initialization if (!commonjsGlobal[gracefulQueue]) { // This queue can be shared by multiple loaded instances var queue = []; Object.defineProperty(commonjsGlobal, gracefulQueue, { get: function() { return queue } }); // Patch fs.close/closeSync to shared queue version, because we need // to retry() whenever a close happens *anywhere* in the program. // This is essential when multiple graceful-fs instances are // in play at the same time. fs.close = (function (fs$close) { function close (fd, cb) { return fs$close.call(fs, fd, function (err) { // This function uses the graceful-fs shared queue if (!err) { retry(); } if (typeof cb === 'function') cb.apply(this, arguments); }) } Object.defineProperty(close, previousSymbol, { value: fs$close }); return close })(fs.close); fs.closeSync = (function (fs$closeSync) { function closeSync (fd) { // This function uses the graceful-fs shared queue fs$closeSync.apply(fs, arguments); retry(); } Object.defineProperty(closeSync, previousSymbol, { value: fs$closeSync }); return closeSync })(fs.closeSync); if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || '')) { process.on('exit', function() { debug(commonjsGlobal[gracefulQueue]); assert.equal(commonjsGlobal[gracefulQueue].length, 0); }); } } module.exports = patch(clone_1(fs)); if (process.env.TEST_GRACEFUL_FS_GLOBAL_PATCH && !fs.__patched) { module.exports = patch(fs); fs.__patched = true; } function patch (fs) { // Everything that references the open() function needs to be in here polyfills(fs); fs.gracefulify = patch; fs.createReadStream = createReadStream; fs.createWriteStream = createWriteStream; var fs$readFile = fs.readFile; fs.readFile = readFile; function readFile (path, options, cb) { if (typeof options === 'function') cb = options, options = null; return go$readFile(path, options, cb) function go$readFile (path, options, cb) { return fs$readFile(path, options, function (err) { if (err && (err.code === 'EMFILE' || err.code === 'ENFILE')) enqueue([go$readFile, [path, options, cb]]); else { if (typeof cb === 'function') cb.apply(this, arguments); retry(); } }) } } var fs$writeFile = fs.writeFile; fs.writeFile = writeFile; function writeFile (path, data, options, cb) { if (typeof options === 'function') cb = options, options = null; return go$writeFile(path, data, options, cb) function go$writeFile (path, data, options, cb) { return fs$writeFile(path, data, options, function (err) { if (err && (err.code === 'EMFILE' || err.code === 'ENFILE')) enqueue([go$writeFile, [path, data, options, cb]]); else { if (typeof cb === 'function') cb.apply(this, arguments); retry(); } }) } } var fs$appendFile = fs.appendFile; if (fs$appendFile) fs.appendFile = appendFile; function appendFile (path, data, options, cb) { if (typeof options === 'function') cb = options, options = null; return go$appendFile(path, data, options, cb) function go$appendFile (path, data, options, cb) { return fs$appendFile(path, data, options, function (err) { if (err && (err.code === 'EMFILE' || err.code === 'ENFILE')) enqueue([go$appendFile, [path, data, options, cb]]); else { if (typeof cb === 'function') cb.apply(this, arguments); retry(); } }) } } var fs$readdir = fs.readdir; fs.readdir = readdir; function readdir (path, options, cb) { var args = [path]; if (typeof options !== 'function') { args.push(options); } else { cb = options; } args.push(go$readdir$cb); return go$readdir(args) function go$readdir$cb (err, files) { if (files && files.sort) files.sort(); if (err && (err.code === 'EMFILE' || err.code === 'ENFILE')) enqueue([go$readdir, [args]]); else { if (typeof cb === 'function') cb.apply(this, arguments); retry(); } } } function go$readdir (args) { return fs$readdir.apply(fs, args) } if (process.version.substr(0, 4) === 'v0.8') { var legStreams = legacyStreams(fs); ReadStream = legStreams.ReadStream; WriteStream = legStreams.WriteStream; } var fs$ReadStream = fs.ReadStream; if (fs$ReadStream) { ReadStream.prototype = Object.create(fs$ReadStream.prototype); ReadStream.prototype.open = ReadStream$open; } var fs$WriteStream = fs.WriteStream; if (fs$WriteStream) { WriteStream.prototype = Object.create(fs$WriteStream.prototype); WriteStream.prototype.open = WriteStream$open; } Object.defineProperty(fs, 'ReadStream', { get: function () { return ReadStream }, set: function (val) { ReadStream = val; }, enumerable: true, configurable: true }); Object.defineProperty(fs, 'WriteStream', { get: function () { return WriteStream }, set: function (val) { WriteStream = val; }, enumerable: true, configurable: true }); // legacy names var FileReadStream = ReadStream; Object.defineProperty(fs, 'FileReadStream', { get: function () { return FileReadStream }, set: function (val) { FileReadStream = val; }, enumerable: true, configurable: true }); var FileWriteStream = WriteStream; Object.defineProperty(fs, 'FileWriteStream', { get: function () { return FileWriteStream }, set: function (val) { FileWriteStream = val; }, enumerable: true, configurable: true }); function ReadStream (path, options) { if (this instanceof ReadStream) return fs$ReadStream.apply(this, arguments), this else return ReadStream.apply(Object.create(ReadStream.prototype), arguments) } function ReadStream$open () { var that = this; open(that.path, that.flags, that.mode, function (err, fd) { if (err) { if (that.autoClose) that.destroy(); that.emit('error', err); } else { that.fd = fd; that.emit('open', fd); that.read(); } }); } function WriteStream (path, options) { if (this instanceof WriteStream) return fs$WriteStream.apply(this, arguments), this else return WriteStream.apply(Object.create(WriteStream.prototype), arguments) } function WriteStream$open () { var that = this; open(that.path, that.flags, that.mode, function (err, fd) { if (err) { that.destroy(); that.emit('error', err); } else { that.fd = fd; that.emit('open', fd); } }); } function createReadStream (path, options) { return new fs.ReadStream(path, options) } function createWriteStream (path, options) { return new fs.WriteStream(path, options) } var fs$open = fs.open; fs.open = open; function open (path, flags, mode, cb) { if (typeof mode === 'function') cb = mode, mode = null; return go$open(path, flags, mode, cb) function go$open (path, flags, mode, cb) { return fs$open(path, flags, mode, function (err, fd) { if (err && (err.code === 'EMFILE' || err.code === 'ENFILE')) enqueue([go$open, [path, flags, mode, cb]]); else { if (typeof cb === 'function') cb.apply(this, arguments); retry(); } }) } } return fs } function enqueue (elem) { debug('ENQUEUE', elem[0].name, elem[1]); commonjsGlobal[gracefulQueue].push(elem); } function retry () { var elem = commonjsGlobal[gracefulQueue].shift(); if (elem) { debug('RETRY', elem[0].name, elem[1]); elem[0].apply(null, elem[1]); } } }); var fs_1 = createCommonjsModule(function (module, exports) { // This is adapted from https://github.com/normalize/mz // Copyright (c) 2014-2016 Jonathan Ong me@jongleberry.com and Contributors const u = universalify.fromCallback; const api = [ 'access', 'appendFile', 'chmod', 'chown', 'close', 'copyFile', 'fchmod', 'fchown', 'fdatasync', 'fstat', 'fsync', 'ftruncate', 'futimes', 'lchown', 'lchmod', 'link', 'lstat', 'mkdir', 'mkdtemp', 'open', 'readFile', 'readdir', 'readlink', 'realpath', 'rename', 'rmdir', 'stat', 'symlink', 'truncate', 'unlink', 'utimes', 'writeFile' ].filter(key => { // Some commands are not available on some systems. Ex: // fs.copyFile was added in Node.js v8.5.0 // fs.mkdtemp was added in Node.js v5.10.0 // fs.lchown is not available on at least some Linux return typeof gracefulFs[key] === 'function' }); // Export all keys: Object.keys(gracefulFs).forEach(key => { if (key === 'promises') { // fs.promises is a getter property that triggers ExperimentalWarning // Don't re-export it here, the getter is defined in "lib/index.js" return } exports[key] = gracefulFs[key]; }); // Universalify async methods: api.forEach(method => { exports[method] = u(gracefulFs[method]); }); // We differ from mz/fs in that we still ship the old, broken, fs.exists() // since we are a drop-in replacement for the native module exports.exists = function (filename, callback) { if (typeof callback === 'function') { return gracefulFs.exists(filename, callback) } return new Promise(resolve => { return gracefulFs.exists(filename, resolve) }) }; // fs.read() & fs.write need special treatment due to multiple callback args exports.read = function (fd, buffer, offset, length, position, callback) { if (typeof callback === 'function') { return gracefulFs.read(fd, buffer, offset, length, position, callback) } return new Promise((resolve, reject) => { gracefulFs.read(fd, buffer, offset, length, position, (err, bytesRead, buffer) => { if (err) return reject(err) resolve({ bytesRead, buffer }); }); }) }; // Function signature can be // fs.write(fd, buffer[, offset[, length[, position]]], callback) // OR // fs.write(fd, string[, position[, encoding]], callback) // We need to handle both cases, so we use ...args exports.write = function (fd, buffer, ...args) { if (typeof args[args.length - 1] === 'function') { return gracefulFs.write(fd, buffer, ...args) } return new Promise((resolve, reject) => { gracefulFs.write(fd, buffer, ...args, (err, bytesWritten, buffer) => { if (err) return reject(err) resolve({ bytesWritten, buffer }); }); }) }; // fs.realpath.native only available in Node v9.2+ if (typeof gracefulFs.realpath.native === 'function') { exports.realpath.native = u(gracefulFs.realpath.native); } }); var fs_2 = fs_1.exists; var fs_3 = fs_1.read; var fs_4 = fs_1.write; // get drive on windows function getRootPath (p) { p = path__default.normalize(path__default.resolve(p)).split(path__default.sep); if (p.length > 0) return p[0] return null } // http://stackoverflow.com/a/62888/10333 contains more accurate // TODO: expand to include the rest const INVALID_PATH_CHARS = /[<>:"|?*]/; function invalidWin32Path (p) { const rp = getRootPath(p); p = p.replace(rp, ''); return INVALID_PATH_CHARS.test(p) } var win32 = { getRootPath, invalidWin32Path }; const invalidWin32Path$1 = win32.invalidWin32Path; const o777 = parseInt('0777', 8); function mkdirs (p, opts, callback, made) { if (typeof opts === 'function') { callback = opts; opts = {}; } else if (!opts || typeof opts !== 'object') { opts = { mode: opts }; } if (process.platform === 'win32' && invalidWin32Path$1(p)) { const errInval = new Error(p + ' contains invalid WIN32 path characters.'); errInval.code = 'EINVAL'; return callback(errInval) } let mode = opts.mode; const xfs = opts.fs || gracefulFs; if (mode === undefined) { mode = o777 & (~process.umask()); } if (!made) made = null; callback = callback || function () {}; p = path__default.resolve(p); xfs.mkdir(p, mode, er => { if (!er) { made = made || p; return callback(null, made) } switch (er.code) { case 'ENOENT': if (path__default.dirname(p) === p) return callback(er) mkdirs(path__default.dirname(p), opts, (er, made) => { if (er) callback(er, made); else mkdirs(p, opts, callback, made); }); break // In the case of any other error, just see if there's a dir // there already. If so, then hooray! If not, then something // is borked. default: xfs.stat(p, (er2, stat) => { // if the stat fails, then that's super weird. // let the original error be the failure reason. if (er2 || !stat.isDirectory()) callback(er, made); else callback(null, made); }); break } }); } var mkdirs_1 = mkdirs; const invalidWin32Path$2 = win32.invalidWin32Path; const o777$1 = parseInt('0777', 8); function mkdirsSync (p, opts, made) { if (!opts || typeof opts !== 'object') { opts = { mode: opts }; } let mode = opts.mode; const xfs = opts.fs || gracefulFs; if (process.platform === 'win32' && invalidWin32Path$2(p)) { const errInval = new Error(p + ' contains invalid WIN32 path characters.'); errInval.code = 'EINVAL'; throw errInval } if (mode === undefined) { mode = o777$1 & (~process.umask()); } if (!made) made = null; p = path__default.resolve(p); try { xfs.mkdirSync(p, mode); made = made || p; } catch (err0) { if (err0.code === 'ENOENT') { if (path__default.dirname(p) === p) throw err0 made = mkdirsSync(path__default.dirname(p), opts, made); mkdirsSync(p, opts, made); } else { // In the case of any other error, just see if there's a dir there // already. If so, then hooray! If not, then something is borked. let stat; try { stat = xfs.statSync(p); } catch (err1) { throw err0 } if (!stat.isDirectory()) throw err0 } } return made } var mkdirsSync_1 = mkdirsSync; const u = universalify.fromCallback; const mkdirs$1 = u(mkdirs_1); var mkdirs_1$1 = { mkdirs: mkdirs$1, mkdirsSync: mkdirsSync_1, // alias mkdirp: mkdirs$1, mkdirpSync: mkdirsSync_1, ensureDir: mkdirs$1, ensureDirSync: mkdirsSync_1 }; // HFS, ext{2,3}, FAT do not, Node.js v0.10 does not function hasMillisResSync () { let tmpfile = path__default.join('millis-test-sync' + Date.now().toString() + Math.random().toString().slice(2)); tmpfile = path__default.join(os.tmpdir(), tmpfile); // 550 millis past UNIX epoch const d = new Date(1435410243862); gracefulFs.writeFileSync(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141'); const fd = gracefulFs.openSync(tmpfile, 'r+'); gracefulFs.futimesSync(fd, d, d); gracefulFs.closeSync(fd); return gracefulFs.statSync(tmpfile).mtime > 1435410243000 } function hasMillisRes (callback) { let tmpfile = path__default.join('millis-test' + Date.now().toString() + Math.random().toString().slice(2)); tmpfile = path__default.join(os.tmpdir(), tmpfile); // 550 millis past UNIX epoch const d = new Date(1435410243862); gracefulFs.writeFile(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141', err => { if (err) return callback(err) gracefulFs.open(tmpfile, 'r+', (err, fd) => { if (err) return callback(err) gracefulFs.futimes(fd, d, d, err => { if (err) return callback(err) gracefulFs.close(fd, err => { if (err) return callback(err) gracefulFs.stat(tmpfile, (err, stats) => { if (err) return callback(err) callback(null, stats.mtime > 1435410243000); }); }); }); }); }); } function timeRemoveMillis (timestamp) { if (typeof timestamp === 'number') { return Math.floor(timestamp / 1000) * 1000 } else if (timestamp instanceof Date) { return new Date(Math.floor(timestamp.getTime() / 1000) * 1000) } else { throw new Error('fs-extra: timeRemoveMillis() unknown parameter type') } } function utimesMillis (path, atime, mtime, callback) { // if (!HAS_MILLIS_RES) return fs.utimes(path, atime, mtime, callback) gracefulFs.open(path, 'r+', (err, fd) => { if (err) return callback(err) gracefulFs.futimes(fd, atime, mtime, futimesErr => { gracefulFs.close(fd, closeErr => { if (callback) callback(futimesErr || closeErr); }); }); }); } function utimesMillisSync (path, atime, mtime) { const fd = gracefulFs.openSync(path, 'r+'); gracefulFs.futimesSync(fd, atime, mtime); return gracefulFs.closeSync(fd) } var utimes = { hasMillisRes, hasMillisResSync, timeRemoveMillis, utimesMillis, utimesMillisSync }; const NODE_VERSION_MAJOR_WITH_BIGINT = 10; const NODE_VERSION_MINOR_WITH_BIGINT = 5; const NODE_VERSION_PATCH_WITH_BIGINT = 0; const nodeVersion = process.versions.node.split('.'); const nodeVersionMajor = Number.parseInt(nodeVersion[0], 10); const nodeVersionMinor = Number.parseInt(nodeVersion[1], 10); const nodeVersionPatch = Number.parseInt(nodeVersion[2], 10); function nodeSupportsBigInt () { if (nodeVersionMajor > NODE_VERSION_MAJOR_WITH_BIGINT) { return true } else if (nodeVersionMajor === NODE_VERSION_MAJOR_WITH_BIGINT) { if (nodeVersionMinor > NODE_VERSION_MINOR_WITH_BIGINT) { return true } else if (nodeVersionMinor === NODE_VERSION_MINOR_WITH_BIGINT) { if (nodeVersionPatch >= NODE_VERSION_PATCH_WITH_BIGINT) { return true } } } return false } function getStats (src, dest, cb) { if (nodeSupportsBigInt()) { gracefulFs.stat(src, { bigint: true }, (err, srcStat) => { if (err) return cb(err) gracefulFs.stat(dest, { bigint: true }, (err, destStat) => { if (err) { if (err.code === 'ENOENT') return cb(null, { srcStat, destStat: null }) return cb(err) } return cb(null, { srcStat, destStat }) }); }); } else { gracefulFs.stat(src, (err, srcStat) => { if (err) return cb(err) gracefulFs.stat(dest, (err, destStat) => { if (err) { if (err.code === 'ENOENT') return cb(null, { srcStat, destStat: null }) return cb(err) } return cb(null, { srcStat, destStat }) }); }); } } function getStatsSync (src, dest) { let srcStat, destStat; if (nodeSupportsBigInt()) { srcStat = gracefulFs.statSync(src, { bigint: true }); } else { srcStat = gracefulFs.statSync(src); } try { if (nodeSupportsBigInt()) { destStat = gracefulFs.statSync(dest, { bigint: true }); } else { destStat = gracefulFs.statSync(dest); } } catch (err) { if (err.code === 'ENOENT') return { srcStat, destStat: null } throw err } return { srcStat, destStat } } function checkPaths (src, dest, funcName, cb) { getStats(src, dest, (err, stats) => { if (err) return cb(err) const { srcStat, destStat } = stats; if (destStat && destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev) { return cb(new Error('Source and destination must not be the same.')) } if (srcStat.isDirectory() && isSrcSubdir(src, dest)) { return cb(new Error(errMsg(src, dest, funcName))) } return cb(null, { srcStat, destStat }) }); } function checkPathsSync (src, dest, funcName) { const { srcStat, destStat } = getStatsSync(src, dest); if (destStat && destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev) { throw new Error('Source and destination must not be the same.') } if (srcStat.isDirectory() && isSrcSubdir(src, dest)) { throw new Error(errMsg(src, dest, funcName)) } return { srcStat, destStat } } // recursively check if dest parent is a subdirectory of src. // It works for all file types including symlinks since it // checks the src and dest inodes. It starts from the deepest // parent and stops once it reaches the src parent or the root path. function checkParentPaths (src, srcStat, dest, funcName, cb) { const srcParent = path__default.resolve(path__default.dirname(src)); const destParent = path__default.resolve(path__default.dirname(dest)); if (destParent === srcParent || destParent === path__default.parse(destParent).root) return cb() if (nodeSupportsBigInt()) { gracefulFs.stat(destParent, { bigint: true }, (err, destStat) => { if (err) { if (err.code === 'ENOENT') return cb() return cb(err) } if (destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev) { return cb(new Error(errMsg(src, dest, funcName))) } return checkParentPaths(src, srcStat, destParent, funcName, cb) }); } else { gracefulFs.stat(destParent, (err, destStat) => { if (err) { if (err.code === 'ENOENT') return cb() return cb(err) } if (destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev) { return cb(new Error(errMsg(src, dest, funcName))) } return checkParentPaths(src, srcStat, destParent, funcName, cb) }); } } function checkParentPathsSync (src, srcStat, dest, funcName) { const srcParent = path__default.resolve(path__default.dirname(src)); const destParent = path__default.resolve(path__default.dirname(dest)); if (destParent === srcParent || destParent === path__default.parse(destParent).root) return let destStat; try { if (nodeSupportsBigInt()) { destStat = gracefulFs.statSync(destParent, { bigint: true }); } else { destStat = gracefulFs.statSync(destParent); } } catch (err) { if (err.code === 'ENOENT') return throw err } if (destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev) { throw new Error(errMsg(src, dest, funcName)) } return checkParentPathsSync(src, srcStat, destParent, funcName) } // return true if dest is a subdir of src, otherwise false. // It only checks the path strings. function isSrcSubdir (src, dest) { const srcArr = path__default.resolve(src).split(path__default.sep).filter(i => i); const destArr = path__default.resolve(dest).split(path__default.sep).filter(i => i); return srcArr.reduce((acc, cur, i) => acc && destArr[i] === cur, true) } function errMsg (src, dest, funcName) { return `Cannot ${funcName} '${src}' to a subdirectory of itself, '${dest}'.` } var stat = { checkPaths, checkPathsSync, checkParentPaths, checkParentPathsSync, isSrcSubdir }; /* eslint-disable node/no-deprecated-api */ var buffer = function (size) { if (typeof Buffer.allocUnsafe === 'function') { try { return Buffer.allocUnsafe(size) } catch (e) { return new Buffer(size) } } return new Buffer(size) }; const mkdirpSync = mkdirs_1$1.mkdirsSync; const utimesSync = utimes.utimesMillisSync; function copySync (src, dest, opts) { if (typeof opts === 'function') { opts = { filter: opts }; } opts = opts || {}; opts.clobber = 'clobber' in opts ? !!opts.clobber : true; // default to true for now opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber; // overwrite falls back to clobber // Warn about using preserveTimestamps on 32-bit node if (opts.preserveTimestamps && process.arch === 'ia32') { console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n see https://github.com/jprichardson/node-fs-extra/issues/269`); } const { srcStat, destStat } = stat.checkPathsSync(src, dest, 'copy'); stat.checkParentPathsSync(src, srcStat, dest, 'copy'); return handleFilterAndCopy(destStat, src, dest, opts) } function handleFilterAndCopy (destStat, src, dest, opts) { if (opts.filter && !opts.filter(src, dest)) return const destParent = path__default.dirname(dest); if (!gracefulFs.existsSync(destParent)) mkdirpSync(destParent); return startCopy(destStat, src, dest, opts) } function startCopy (destStat, src, dest, opts) { if (opts.filter && !opts.filter(src, dest)) return return getStats$1(destStat, src, dest, opts) } function getStats$1 (destStat, src, dest, opts) { const statSync = opts.dereference ? gracefulFs.statSync : gracefulFs.lstatSync; const srcStat = statSync(src); if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts) else if (srcStat.isFile() || srcStat.isCharacterDevice() || srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts) else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts) } function onFile (srcStat, destStat, src, dest, opts) { if (!destStat) return copyFile(srcStat, src, dest, opts) return mayCopyFile(srcStat, src, dest, opts) } function mayCopyFile (srcStat, src, dest, opts) { if (opts.overwrite) { gracefulFs.unlinkSync(dest); return copyFile(srcStat, src, dest, opts) } else if (opts.errorOnExist) { throw new Error(`'${dest}' already exists`) } } function copyFile (srcStat, src, dest, opts) { if (typeof gracefulFs.copyFileSync === 'function') { gracefulFs.copyFileSync(src, dest); gracefulFs.chmodSync(dest, srcStat.mode); if (opts.preserveTimestamps) { return utimesSync(dest, srcStat.atime, srcStat.mtime) } return } return copyFileFallback(srcStat, src, dest, opts) } function copyFileFallback (srcStat, src, dest, opts) { const BUF_LENGTH = 64 * 1024; const _buff = buffer(BUF_LENGTH); const fdr = gracefulFs.openSync(src, 'r'); const fdw = gracefulFs.openSync(dest, 'w', srcStat.mode); let pos = 0; while (pos < srcStat.size) { const bytesRead = gracefulFs.readSync(fdr, _buff, 0, BUF_LENGTH, pos); gracefulFs.writeSync(fdw, _buff, 0, bytesRead); pos += bytesRead; } if (opts.preserveTimestamps) gracefulFs.futimesSync(fdw, srcStat.atime, srcStat.mtime); gracefulFs.closeSync(fdr); gracefulFs.closeSync(fdw); } function onDir (srcStat, destStat, src, dest, opts) { if (!destStat) return mkDirAndCopy(srcStat, src, dest, opts) if (destStat && !destStat.isDirectory()) { throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`) } return copyDir(src, dest, opts) } function mkDirAndCopy (srcStat, src, dest, opts) { gracefulFs.mkdirSync(dest); copyDir(src, dest, opts); return gracefulFs.chmodSync(dest, srcStat.mode) } function copyDir (src, dest, opts) { gracefulFs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts)); } function copyDirItem (item, src, dest, opts) { const srcItem = path__default.join(src, item); const destItem = path__default.join(dest, item); const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy'); return startCopy(destStat, srcItem, destItem, opts) } function onLink (destStat, src, dest, opts) { let resolvedSrc = gracefulFs.readlinkSync(src); if (opts.dereference) { resolvedSrc = path__default.resolve(process.cwd(), resolvedSrc); } if (!destStat) { return gracefulFs.symlinkSync(resolvedSrc, dest) } else { let resolvedDest; try { resolvedDest = gracefulFs.readlinkSync(dest); } catch (err) { // dest exists and is a regular file or directory, // Windows may throw UNKNOWN error. If dest already exists, // fs throws error anyway, so no need to guard against it here. if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return gracefulFs.symlinkSync(resolvedSrc, dest) throw err } if (opts.dereference) { resolvedDest = path__default.resolve(process.cwd(), resolvedDest); } if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) { throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`) } // prevent copy if src is a subdir of dest since unlinking // dest in this case would result in removing src contents // and therefore a broken symlink would be created. if (gracefulFs.statSync(dest).isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) { throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`) } return copyLink(resolvedSrc, dest) } } function copyLink (resolvedSrc, dest) { gracefulFs.unlinkSync(dest); return gracefulFs.symlinkSync(resolvedSrc, dest) } var copySync_1 = copySync; var copySync$1 = { copySync: copySync_1 }; const u$1 = universalify.fromPromise; function pathExists (path) { return fs_1.access(path).then(() => true).catch(() => false) } var pathExists_1 = { pathExists: u$1(pathExists), pathExistsSync: fs_1.existsSync }; const mkdirp = mkdirs_1$1.mkdirs; const pathExists$1 = pathExists_1.pathExists; const utimes$1 = utimes.utimesMillis; function copy (src, dest, opts, cb) { if (typeof opts === 'function' && !cb) { cb = opts; opts = {}; } else if (typeof opts === 'function') { opts = { filter: opts }; } cb = cb || function () {}; opts = opts || {}; opts.clobber = 'clobber' in opts ? !!opts.clobber : true; // default to true for now opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber; // overwrite falls back to clobber // Warn about using preserveTimestamps on 32-bit node if (opts.preserveTimestamps && process.arch === 'ia32') { console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n see https://github.com/jprichardson/node-fs-extra/issues/269`); } stat.checkPaths(src, dest, 'copy', (err, stats) => { if (err) return cb(err) const { srcStat, destStat } = stats; stat.checkParentPaths(src, srcStat, dest, 'copy', err => { if (err) return cb(err) if (opts.filter) return handleFilter(checkParentDir, destStat, src, dest,