UNPKG

npmize

Version:

Let's create an npm package without worrying about anything.

56 lines (55 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TSImportType = TSImportType; exports.ImportDeclaration_ExportNamedDeclaration_ExportAllDeclaration = ImportDeclaration_ExportNamedDeclaration_ExportAllDeclaration; exports.CallExpressionImport = CallExpressionImport; exports.CallExpressionRequire = CallExpressionRequire; function parseString(str) { return { start: str.start, end: str.end, value: str.value, }; } function isOkString(node) { return Boolean(node && node.type === 'StringLiteral'); } function findNestedItems(entireObj, valToFind) { const foundObj = []; JSON.stringify(entireObj, (_, nestedValue) => { const found = nestedValue && nestedValue.type === valToFind; found && foundObj.push(nestedValue); return nestedValue; }); return foundObj; } function TSImportType(parsed) { return findNestedItems(parsed, 'TSImportType') .filter((node) => isOkString(node.argument)) .map((node) => parseString(node.argument)); } function ImportDeclaration_ExportNamedDeclaration_ExportAllDeclaration(parsed) { return [ findNestedItems(parsed, 'ImportDeclaration'), findNestedItems(parsed, 'ExportDeclaration'), findNestedItems(parsed, 'ExportNamedDeclaration'), findNestedItems(parsed, 'ExportAllDeclaration'), ] .flat() .filter((node) => isOkString(node.source)) .map((node) => parseString(node.source)); } function CallExpressionImport(parsed) { return findNestedItems(parsed, 'CallExpression') .filter((node) => node && node.callee && node.callee.type === 'Import' && isOkString(node.arguments[0])) .map((node) => parseString(node.arguments[0])); } function CallExpressionRequire(parsed) { return findNestedItems(parsed, 'CallExpression') .filter((node) => node && node.callee && node.callee.type === 'Identifier' && node.callee.name === 'require' && isOkString(node.arguments[0])) .map((node) => parseString(node.arguments[0])); }