UNPKG

get-amd-module-type

Version:

Get the type of an AMD module used for an AST node or within a file

103 lines (85 loc) 2.26 kB
import fs from 'node:fs'; import Walker from 'node-source-walk'; import { isNamedForm, isDependencyForm, isREMForm, isFactoryForm, isNoDependencyForm, isAMDDriverScriptRequire } from 'ast-module-types'; /** * Asynchronously identifies the AMD module type of the given file. * * @param {string} filename * @param {Function} callback - Called with (error, type) * * @example * define('name', [deps], func) // 'named' * define([deps], func) // 'deps' * define(func(require)) // 'factory' * define({}) // 'nodeps' */ function getType(filename, callback) { if (!filename) throw new Error('filename missing'); if (!callback) throw new Error('callback missing'); // eslint-disable-next-line n/prefer-promises/fs fs.readFile(filename, 'utf8', (error, data) => { if (error) return callback(error); let type; try { type = fromSource(data); } catch(error) { return callback(error); } callback(null, type); }); } /** * Determines the AMD module type from an AST node. * * @param {Object} node * @returns {string|null} */ function fromAST(node) { if (isNamedForm(node)) return 'named'; if (isDependencyForm(node)) return 'deps'; if (isREMForm(node)) return 'rem'; if (isFactoryForm(node)) return 'factory'; if (isNoDependencyForm(node)) return 'nodeps'; if (isAMDDriverScriptRequire(node)) return 'driver'; return null; } /** * Determines the AMD module type by walking the given source code's AST. * * @param {string} source * @returns {string|null} */ function fromSource(source) { if (source === undefined) throw new Error('source missing'); const walker = new Walker(); let type; walker.walk(source, node => { type = fromAST(node); if (type) { walker.stopWalking(); } }); return type; } /** * Synchronously determines the AMD module type of the given file. * * @param {string} filename * @returns {string|null} */ function sync(filename) { if (!filename) throw new Error('filename missing'); const source = fs.readFileSync(filename, 'utf8'); return fromSource(source); } getType.fromAST = fromAST; getType.fromSource = fromSource; getType.sync = sync; export default getType;