mirror of
https://github.com/fooflington/selfdefined.git
synced 2025-12-16 00:59:05 +00:00
update
This commit is contained in:
55
node_modules/module-definition/Readme.md
generated
vendored
Normal file
55
node_modules/module-definition/Readme.md
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
# module-definition [](https://npmjs.org/package/module-definition) [](https://npmjs.org/package/module-definition)
|
||||
|
||||
Determines the module definition type (CommonJS, AMD, ES6, or none) for a given JavaScript file
|
||||
by walking through the AST.
|
||||
|
||||
`npm install module-definition`
|
||||
|
||||
### Usage
|
||||
|
||||
```javascript
|
||||
var getModuleType = require('module-definition');
|
||||
|
||||
// Async
|
||||
getModuleType('myscript.js', function (err, type) {
|
||||
console.log(type);
|
||||
});
|
||||
|
||||
// Sync
|
||||
var type = getModuleType.sync('myscript.js');
|
||||
console.log(type);
|
||||
|
||||
// From source (string or an AST)
|
||||
var type = getModuleType.fromSource('define({foo: "foo"});');
|
||||
console.log(type);
|
||||
```
|
||||
|
||||
Passes one of the following strings to the given callback or returns the string in sync api:
|
||||
|
||||
* amd
|
||||
* commonjs
|
||||
* es6
|
||||
* none
|
||||
|
||||
* You may also pass an AST to `fromSource` to avoid an internal parsing of the source
|
||||
|
||||
When specifying a filename, using the sync or async api, you can also provide an `options` object with an alternative `fs` implementation used to read the source file with.
|
||||
|
||||
```javascript
|
||||
var myFs = GetFs();
|
||||
var options = {fileSystem: myFs}
|
||||
|
||||
// Async
|
||||
getModuleType('myscript.js', function (err, type) {
|
||||
console.log(type);
|
||||
}, options);
|
||||
|
||||
// Sync
|
||||
var type = getModuleType.sync('myscript.js', options);
|
||||
|
||||
```
|
||||
|
||||
Via shell command (requires a global install: `npm install -g module-definition`)
|
||||
```
|
||||
module-definition filename
|
||||
```
|
||||
8
node_modules/module-definition/bin/module-definition.js
generated
vendored
Executable file
8
node_modules/module-definition/bin/module-definition.js
generated
vendored
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
|
||||
var getModuleType = require('../');
|
||||
var filename = process.argv[2];
|
||||
|
||||
console.log(getModuleType.sync(filename));
|
||||
127
node_modules/module-definition/index.js
generated
vendored
Normal file
127
node_modules/module-definition/index.js
generated
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
'use strict';
|
||||
|
||||
const Walker = require('node-source-walk');
|
||||
const types = require('ast-module-types');
|
||||
const fs = require('fs');
|
||||
|
||||
/**
|
||||
* Determines the type of the module from the supplied source code or AST
|
||||
*
|
||||
* @param {String|Object} source - The string content or AST of a file
|
||||
* @return {String}
|
||||
*/
|
||||
function fromSource(source) {
|
||||
if (typeof source === 'undefined') {
|
||||
throw new Error('source not supplied');
|
||||
}
|
||||
|
||||
const walker = new Walker();
|
||||
let type = 'none';
|
||||
let hasDefine = false;
|
||||
let hasAMDTopLevelRequire = false;
|
||||
let hasRequire = false;
|
||||
let hasExports = false;
|
||||
let hasES6Import = false;
|
||||
let hasES6Export = false;
|
||||
|
||||
// Walker accepts as AST to avoid reparsing
|
||||
walker.walk(source, function(node) {
|
||||
if (types.isDefine(node)) {
|
||||
hasDefine = true;
|
||||
}
|
||||
|
||||
if (types.isRequire(node)) {
|
||||
hasRequire = true;
|
||||
}
|
||||
|
||||
if (types.isExports(node)) {
|
||||
hasExports = true;
|
||||
}
|
||||
|
||||
if (types.isAMDDriverScriptRequire(node)) {
|
||||
hasAMDTopLevelRequire = true;
|
||||
}
|
||||
|
||||
if (types.isES6Import(node)) {
|
||||
hasES6Import = true;
|
||||
}
|
||||
|
||||
if (types.isES6Export(node)) {
|
||||
hasES6Export = true;
|
||||
}
|
||||
|
||||
if (hasES6Import || hasES6Export) {
|
||||
type = 'es6';
|
||||
walker.stopWalking();
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasDefine || hasAMDTopLevelRequire) {
|
||||
type = 'amd';
|
||||
walker.stopWalking();
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasExports || (hasRequire && !hasDefine)) {
|
||||
type = 'commonjs';
|
||||
walker.stopWalking();
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously determine the module type for the contents of the passed filepath
|
||||
*
|
||||
* @param {String} file
|
||||
* @param {Object} options
|
||||
* @return {String}
|
||||
*/
|
||||
function sync(file, options) {
|
||||
if (!file) {
|
||||
throw new Error('filename missing');
|
||||
}
|
||||
var fileSystem = options ? (options.fileSystem || fs) : fs;
|
||||
const data = fileSystem.readFileSync(file, 'utf8');
|
||||
return fromSource(data.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously determines the module type for the contents of the given filepath
|
||||
*
|
||||
* @param {String} filepath
|
||||
* @param {Function} cb - Executed with (err, type)
|
||||
*/
|
||||
module.exports = function(filepath, cb, options) {
|
||||
if (!filepath) {
|
||||
throw new Error('filename missing');
|
||||
}
|
||||
|
||||
if (!cb) {
|
||||
throw new Error('callback missing');
|
||||
}
|
||||
|
||||
const opts = {encoding: 'utf8'};
|
||||
var fileSystem = options ? (options.fileSystem || fs) : fs;
|
||||
|
||||
fileSystem.readFile(filepath, opts, function(err, data) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
let type;
|
||||
|
||||
try {
|
||||
type = fromSource(data);
|
||||
} catch (error) {
|
||||
return cb(error);
|
||||
}
|
||||
|
||||
cb(null, type);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.sync = sync;
|
||||
module.exports.fromSource = fromSource;
|
||||
69
node_modules/module-definition/package.json
generated
vendored
Normal file
69
node_modules/module-definition/package.json
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"module-definition@3.2.0",
|
||||
"/Users/tatiana/selfdefined"
|
||||
]
|
||||
],
|
||||
"_from": "module-definition@3.2.0",
|
||||
"_id": "module-definition@3.2.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-PO6o0BajpdRR+fb3FUSeDISgJpnyxg8UDUEalR8LPQajl0M5+m4jHWhgrMGGSEl6D9+sVl/l1fjOCvpBXIQ+2Q==",
|
||||
"_location": "/module-definition",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "module-definition@3.2.0",
|
||||
"name": "module-definition",
|
||||
"escapedName": "module-definition",
|
||||
"rawSpec": "3.2.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "3.2.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/filing-cabinet",
|
||||
"/precinct"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/module-definition/-/module-definition-3.2.0.tgz",
|
||||
"_spec": "3.2.0",
|
||||
"_where": "/Users/tatiana/selfdefined",
|
||||
"author": {
|
||||
"name": "Joel Kemp",
|
||||
"email": "joel@mrjoelkemp.com",
|
||||
"url": "http://www.mrjoelkemp.com/"
|
||||
},
|
||||
"bin": {
|
||||
"module-definition": "bin/module-definition.js"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/mrjoelkemp/module-definition/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"ast-module-types": "^2.4.0",
|
||||
"node-source-walk": "^4.0.0"
|
||||
},
|
||||
"description": "Determines if a file is using a CommonJS or AMD module definition",
|
||||
"devDependencies": {
|
||||
"jscs": "^3.0.7",
|
||||
"jscs-preset-mrjoelkemp": "~2.0.0",
|
||||
"memfs": "^2.14.1",
|
||||
"mocha": "^5.2.0",
|
||||
"unionfs": "^3.0.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6.0"
|
||||
},
|
||||
"homepage": "https://github.com/mrjoelkemp/module-definition",
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "module-definition",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/mrjoelkemp/module-definition.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jscs test/test.js index.js bin/module-definition.js && mocha test/test.js"
|
||||
},
|
||||
"version": "3.2.0"
|
||||
}
|
||||
Reference in New Issue
Block a user