Add and run removeNPMAbsolutePaths

This commit is contained in:
Robert Brignull 2020-05-12 12:11:25 +01:00
parent a76042ab4a
commit 256c63a715
712 changed files with 13722 additions and 18895 deletions

51
node_modules/removeNPMAbsolutePaths/src/cli.js generated vendored Normal file
View file

@ -0,0 +1,51 @@
'use strict';
const defaultOpts = {
force: false,
};
function parseArguments(args) {
if (args.length < 1) {
throw new Error('Missing path.\nThe first argument should be the path to a directory or a package.json file.');
}
const path = args[0];
const opts = { ...defaultOpts };
const ignored = [];
for (let i = 1; i < args.length; i += 1) {
const arg = args[i];
switch (arg) {
case '--force':
opts.force = true;
break;
case '--fields':
if (opts.fields) {
throw new Error('Duplicated argument: --fields.\nThe --fields flag has been detected twice.');
}
opts.fields = [];
while (args[i + 1] && args[i + 1].slice(0, 2) !== '--') {
opts.fields.push(args[i += 1]);
}
if (opts.fields && opts.fields.length === 0) {
throw new Error('Invalid argument usage: --fields.\nThe --fields flag should be followed by the specific fields that should be removed but none was found.');
}
break;
default:
ignored.push(arg);
break;
}
}
return {
path,
opts,
ignored,
};
}
module.exports.defaultOpts = defaultOpts;
module.exports.parseArguments = parseArguments;

310
node_modules/removeNPMAbsolutePaths/src/errno.js generated vendored Normal file
View file

@ -0,0 +1,310 @@
'use strict';
const all = [
{
errno: -2,
code: 'ENOENT',
description: 'No such file or directory',
},
{
errno: -1,
code: 'UNKNOWN',
description: 'Unknown error',
},
{
errno: 0,
code: 'OK',
description: 'Success',
},
{
errno: 1,
code: 'EOF',
description: 'End of file',
},
{
errno: 2,
code: 'EADDRINFO',
description: 'Getaddrinfo error',
},
{
errno: 3,
code: 'EACCES',
description: 'Permission denied',
},
{
errno: 4,
code: 'EAGAIN',
description: 'Resource temporarily unavailable',
},
{
errno: 5,
code: 'EADDRINUSE',
description: 'Address already in use',
},
{
errno: 6,
code: 'EADDRNOTAVAIL',
description: 'Address not available',
},
{
errno: 7,
code: 'EAFNOSUPPORT',
description: 'Address family not supported',
},
{
errno: 8,
code: 'EALREADY',
description: 'Connection already in progress',
},
{
errno: 9,
code: 'EBADF',
description: 'Bad file descriptor',
},
{
errno: 10,
code: 'EBUSY',
description: 'Resource busy or locked',
},
{
errno: 11,
code: 'ECONNABORTED',
description: 'Software caused connection abort',
},
{
errno: 12,
code: 'ECONNREFUSED',
description: 'Connection refused',
},
{
errno: 13,
code: 'ECONNRESET',
description: 'Connection reset by peer',
},
{
errno: 14,
code: 'EDESTADDRREQ',
description: 'Destination address required',
},
{
errno: 15,
code: 'EFAULT',
description: 'Bad address in system call argument',
},
{
errno: 16,
code: 'EHOSTUNREACH',
description: 'Host is unreachable',
},
{
errno: 17,
code: 'EINTR',
description: 'Interrupted system call',
},
{
errno: 18,
code: 'EINVAL',
description: 'Invalid argument',
},
{
errno: 19,
code: 'EISCONN',
description: 'Socket is already connected',
},
{
errno: 20,
code: 'EMFILE',
description: 'Too many open files',
},
{
errno: 21,
code: 'EMSGSIZE',
description: 'Message too long',
},
{
errno: 22,
code: 'ENETDOWN',
description: 'Network is down',
},
{
errno: 23,
code: 'ENETUNREACH',
description: 'Network is unreachable',
},
{
errno: 24,
code: 'ENFILE',
description: 'File table overflow',
},
{
errno: 25,
code: 'ENOBUFS',
description: 'No buffer space available',
},
{
errno: 26,
code: 'ENOMEM',
description: 'Not enough memory',
},
{
errno: 27,
code: 'ENOTDIR',
description: 'Not a directory',
},
{
errno: 28,
code: 'EISDIR',
description: 'Illegal operation on a directory',
},
{
errno: 29,
code: 'ENONET',
description: 'Machine is not on the network',
},
{
errno: 31,
code: 'ENOTCONN',
description: 'Socket is not connected',
},
{
errno: 32,
code: 'ENOTSOCK',
description: 'Socket operation on non-socket',
},
{
errno: 33,
code: 'ENOTSUP',
description: 'Operation not supported on socket',
},
{
errno: 34,
code: 'ENOENT',
description: 'No such file or directory',
},
{
errno: 35,
code: 'ENOSYS',
description: 'Function not implemented',
},
{
errno: 36,
code: 'EPIPE',
description: 'Broken pipe',
},
{
errno: 37,
code: 'EPROTO',
description: 'Protocol error',
},
{
errno: 38,
code: 'EPROTONOSUPPORT',
description: 'Protocol not supported',
},
{
errno: 39,
code: 'EPROTOTYPE',
description: 'Protocol wrong type for socket',
},
{
errno: 40,
code: 'ETIMEDOUT',
description: 'Connection timed out',
},
{
errno: 41,
code: 'ECHARSET',
description: 'Invalid Unicode character',
},
{
errno: 42,
code: 'EAIFAMNOSUPPORT',
description: 'Address family for hostname not supported',
},
{
errno: 44,
code: 'EAISERVICE',
description: 'Servname not supported for ai_socktype',
},
{
errno: 45,
code: 'EAISOCKTYPE',
description: 'Ai_socktype not supported',
},
{
errno: 46,
code: 'ESHUTDOWN',
description: 'Cannot send after transport endpoint shutdown',
},
{
errno: 47,
code: 'EEXIST',
description: 'File already exists',
},
{
errno: 48,
code: 'ESRCH',
description: 'No such process',
},
{
errno: 49,
code: 'ENAMETOOLONG',
description: 'Name too long',
},
{
errno: 50,
code: 'EPERM',
description: 'Operation not permitted',
},
{
errno: 51,
code: 'ELOOP',
description: 'Too many symbolic links encountered',
},
{
errno: 52,
code: 'EXDEV',
description: 'Cross-device link not permitted',
},
{
errno: 53,
code: 'ENOTEMPTY',
description: 'Directory not empty',
},
{
errno: 54,
code: 'ENOSPC',
description: 'No space left on device',
},
{
errno: 55,
code: 'EIO',
description: 'I/O error',
},
{
errno: 56,
code: 'EROFS',
description: 'Read-only file system',
},
{
errno: 57,
code: 'ENODEV',
description: 'No such device',
},
{
errno: 58,
code: 'ESPIPE',
description: 'Invalid seek',
},
{
errno: 59,
code: 'ECANCELED',
description: 'Operation canceled',
},
];
module.exports = {};
all.forEach((error) => {
module.exports[error.errno] = error.description;
});

View file

@ -0,0 +1,137 @@
'use strict';
const path = require('path');
const {
stat, readdir, readFile, writeFile,
} = require('fs');
const { promisify } = require('util');
const errno = require('./errno');
const statAsync = promisify(stat);
const readdirAsync = promisify(readdir);
const readFileAsync = promisify(readFile);
const writeFileAsync = promisify(writeFile);
class ProcessingError extends Error {
constructor(message, err) {
super(message + ((err && err.errno) ? ` (${errno[err.errno]})` : ''));
this.cause = err;
}
}
async function getStats(filePath) {
try {
return await statAsync(filePath);
} catch (err) {
throw new ProcessingError(`Can't read directory/file at "${filePath}"`, err);
}
}
async function processFile(filePath, opts) {
try {
let data;
try {
data = await readFileAsync(filePath, 'utf8');
} catch (err) {
throw new ProcessingError(`Can't read file at "${filePath}"`, err);
}
let shouldWriteFile = false;
let obj;
try {
obj = JSON.parse(data);
} catch (err) {
throw new ProcessingError(`Malformed package.json file at "${filePath}"`, err);
}
Object.keys(obj).forEach((key) => {
const shouldBeDeleted = opts.fields ? (opts.fields.indexOf(key) !== -1) : (key[0] === '_');
if (shouldBeDeleted) {
delete obj[key];
shouldWriteFile = true;
}
});
if (shouldWriteFile || opts.force) {
try {
await writeFileAsync(filePath, JSON.stringify(obj, null, ' '));
} catch (err) {
throw new ProcessingError(`Can't write processed file to "${filePath}"`, err);
}
return { filePath, rewritten: true, success: true };
}
return { filePath, rewritten: false, success: true };
} catch (err) {
return { filePath, err, success: false };
}
}
async function processDir(dirPath, opts) {
try {
let files;
try {
files = await readdirAsync(dirPath);
} catch (err) {
throw new ProcessingError(`Can't read directory at "${dirPath}"`, err);
}
const results = await Promise.all(files.map(async (fileName) => {
const filePath = path.join(dirPath, fileName);
const stats = await getStats(filePath);
if (stats.isDirectory()) {
return processDir(filePath, opts);
}
if (fileName === 'package.json') {
return processFile(filePath, opts);
}
return undefined;
}));
return results.reduce((arr, value) => {
if (!value) {
return arr;
}
if (value.constructor === Array) {
return arr.concat(value);
}
arr.push(value);
return arr;
}, [{ dirPath, success: true }]);
} catch (err) {
return [{ dirPath, err, success: false }];
}
}
async function removeNPMAbsolutePaths(filePath, opts) {
opts = opts || {}; // eslint-disable-line no-param-reassign
if (!filePath) {
throw new ProcessingError('Missing path.\nThe first argument should be the path to a directory or a package.json file.');
}
if (opts.fields && (opts.fields.constructor !== Array || opts.fields.length === 0)) {
throw new ProcessingError('Invalid option: fields.\nThe fields option should be an array cotaining the names of the specific fields that should be removed.');
}
const stats = await getStats(filePath);
if (stats.isDirectory()) {
return processDir(filePath, opts);
}
if (path.basename(filePath) === 'package.json') {
return [await processFile(filePath, opts)];
}
throw new Error('Invalid path provided. The path should be a directory or a package.json file.');
}
module.exports = removeNPMAbsolutePaths;