fib-typify
Advanced tools
Comparing version 0.1.1 to 0.1.2
const fs = require('fs') | ||
const path = require('path') | ||
const vm = require('vm') | ||
const mkdirp = require('@fibjs/mkdirp') | ||
const mm = require('micromatch') | ||
const compileRaw = require('./raw') | ||
const compileFile = require('./fs-file') | ||
const UTILs = require('./_utils') | ||
@@ -31,15 +30,67 @@ const _includeExts = ['.ts'] | ||
const DOT = '.' | ||
function _assertExt (extName) { | ||
if (!extName || extName[0] !== DOT) { | ||
throw `${extName} is not valid extension` | ||
} | ||
return true | ||
} | ||
function _ext2globrule (extname) { | ||
return _assertExt(extname) && `*${extname}` | ||
} | ||
function _getCopyWhiteListViaGlobrule (globRules = []) { | ||
function _handler ({ | ||
baseDir, distDir, filename | ||
}) { | ||
const srcpath = path.resolve(baseDir, filename) | ||
const [_filename] = mm([filename], globRules) | ||
const distpath = path.resolve(distDir, _filename) | ||
if (_filename) { | ||
fs.copy(srcpath, distpath) | ||
} | ||
} | ||
return _handler | ||
} | ||
const DEFAULT_FILEGLOB_TO_COPY = ['*.js', '*.jsc', '*.json'] | ||
const DEFAULT_INCLUDE_GLOB_LIST = ['*', '!node_modules', '!.ts'] | ||
exports.compileTo = function (baseDir, distDir, compileDirToOpts) { | ||
const fileNameList = fs.readdir(baseDir) | ||
if (!fs.exists(distDir)) { | ||
mkdirp(distDir) | ||
UTILs.mkdirp(distDir) | ||
} | ||
const blobNameList = fs.readdir(baseDir) | ||
const { | ||
filterFileName = _getFilenameFilter(), | ||
extsToCopy = undefined, | ||
fileglobsToCopy = DEFAULT_FILEGLOB_TO_COPY, | ||
includeLeveledGlobs = DEFAULT_INCLUDE_GLOB_LIST, | ||
compilerOptions = null | ||
} = compileDirToOpts || {} | ||
fileNameList.forEach((filename) => { | ||
const fullspath = path.resolve(baseDir, filename) | ||
/* deal with old api :start */ | ||
let finalFileGlobsToCopy = fileglobsToCopy | ||
let _globRulesForOldApi | ||
if (Array.isArray(extsToCopy)) { | ||
_globRulesForOldApi = extsToCopy.map(_ext2globrule) | ||
} else if (extsToCopy === '*') { | ||
_globRulesForOldApi = ['*'] | ||
} | ||
/* deal with old api :end */ | ||
if (Array.isArray(_globRulesForOldApi)) { | ||
finalFileGlobsToCopy = _globRulesForOldApi | ||
} | ||
const _onFile = _getCopyWhiteListViaGlobrule(finalFileGlobsToCopy) | ||
const filteredBlobNameList = mm(blobNameList, includeLeveledGlobs) | ||
filteredBlobNameList.forEach((blobname) => { | ||
const fullspath = path.resolve(baseDir, blobname) | ||
const stat = fs.stat(fullspath) | ||
@@ -49,3 +100,3 @@ if (stat.isDirectory()) { | ||
fullspath, | ||
path.resolve(distDir, filename), | ||
path.resolve(distDir, blobname), | ||
compileDirToOpts | ||
@@ -58,7 +109,13 @@ ) | ||
if (!filterFileName(filename)) { | ||
_onFile({ | ||
baseDir, | ||
distDir, | ||
filename: blobname | ||
}) | ||
if (!filterFileName(blobname)) { | ||
return | ||
} | ||
const tbasename = filename.replace('.ts', '.js') | ||
const tbasename = blobname.replace('.ts', '.js') | ||
const tpath = path.join(distDir, tbasename) | ||
@@ -65,0 +122,0 @@ |
const fs = require('fs') | ||
const path = require('path') | ||
const mkdirp = require('@fibjs/mkdirp') | ||
@@ -5,0 +4,0 @@ const CORE = require('./core') |
const fs = require('fs') | ||
const path = require('path') | ||
const vm = require('vm') | ||
const mkdirp = require('@fibjs/mkdirp') | ||
const typescript = require('typescript') | ||
const CORE = require('./core') | ||
const UTILs = require('./_utils') | ||
@@ -18,7 +16,7 @@ exports.compile = function (tsRaw = '', tsCompilerOptions) { | ||
if (tstat.isDirectory()) { | ||
console.warn(`${CORE.logPrefix}targetpath '${targetpath}' is one directory but expected as one file.`) | ||
console.warn(`${UTILs.getLogPrefix('api', 'compileToFile')}targetpath '${targetpath}' is one directory but expected as one file.`) | ||
return | ||
} else if (tstat.isFile()) { | ||
// ONLY FOR DEBUG | ||
console.debug(`${CORE.logPrefix}file '${targetpath}' has existed, try to overwrite it.`) | ||
UTILs.isDebug && console.debug(`${UTILs.getLogPrefix('api', 'compileToFile')}file '${targetpath}' has existed, try to overwrite it.`) | ||
} | ||
@@ -30,5 +28,5 @@ } | ||
try { | ||
mkdirp(basedir) | ||
UTILs.mkdirp(basedir) | ||
} catch (e) { | ||
console.warn(`${CORE.logPrefix}error occured when trying to mkdirp ${basedir}`) | ||
console.warn(`${UTILs.getLogPrefix('api', 'mkdirp')}error occured when trying to mkdirp ${basedir}`) | ||
return | ||
@@ -50,3 +48,3 @@ } | ||
if (!sboxName) { | ||
throw `${CORE.logPrefix}'sboxName' is required for 'sboxOpts'` | ||
throw `${UTILs.getLogPrefix('api', 'compileToFile')}'sboxName' is required for 'sboxOpts'` | ||
} else { | ||
@@ -53,0 +51,0 @@ sbox.addScript(sboxName, new Buffer(compiledString)) |
{ | ||
"name": "fib-typify", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "just write fibjs with typescript : )", | ||
@@ -26,3 +26,4 @@ "main": "index.js", | ||
"@fibjs/mkdirp": "^1.0.1", | ||
"typescript": "^2.8.3" | ||
"micromatch": "^3.1.10", | ||
"typescript": "^2.9.1" | ||
}, | ||
@@ -39,4 +40,5 @@ "ci": { | ||
"devDependencies": { | ||
"@fibjs/ci": "^2.0.0" | ||
"@fibjs/ci": "^2.0.0", | ||
"@types/fibjs": "github:fibjs/fib-types#v1.0.4" | ||
} | ||
} |
@@ -63,7 +63,30 @@ # fib-typify | ||
| compilerOptions | boolean | Y / `false` | typescript's [compilerOptions] | | ||
| filterFileName | (filename: string): boolean | N / - | whether compile File, file would be compiled when returning `true` | | ||
| fileglobsToCopy | Array, '*' | N / `['*.js', '*.jsc', '*.json']` | whitelist for extensions of globnames to copy when recursive walk to one | ||
| includeLeveledGlobs | string | string[] | N / `['*', '!node_modules', '!.ts']` | glob descriptor list to exclude on walk to every directory level, view detail in [micromatch] | | ||
| filterFileName | (filename: string): boolean | N / - | whether compile File, file would be compiled if returning `true` | | ||
<!-- | extsToCopy | Array, '*' | N / `['.js', '.jsc', '.json']` | whitelist for extensions of filename to copy when recursive walk to one file --> | ||
**notice** `compileDirToOpts.extsToCopy` is depreacated, if you pass it,`compileDirToOpts.fileglobsToCopy` get invalid. | ||
### File Filter Priority | ||
High -> Low: | ||
1. includeLeveledGlobs | ||
1. fileglobsToCopy | ||
1. filterFileName | ||
### File Writing Priority | ||
1. [compileResult] | ||
1. fileglobsToCopy | ||
## TODO | ||
- [x] <del>There is no official `*.d.ts` for fibjs yet. I will support generating `fibjs.d.ts` when compilation.</del> Just use [fib-types](https://github.com/fibjs/fib-types) | ||
- [x] <del>There is no official `*.d.ts` for fibjs yet. I will support generating `fibjs.d.ts` when compilation.</del> ---> Just use [fib-types](https://github.com/fibjs/fib-types) | ||
- [ ] better options for `compileDirectoryTo` | ||
- [ ] hooks before, when, after compiling | ||
- [ ] on walk to one file recursively | ||
- [ ] customizable `recursive` | ||
- [ ] support `fileglobsToCopy` with higher priorty than `extsToCopy` | ||
- [ ] compile '.ts' to '.jsc' directly | ||
- [ ] pack compiled '.jsc' to binary and extract one zipped file. | ||
@@ -81,1 +104,2 @@ ## Contributions | ||
[compileDirToOpts]:#compileDirToOpts | ||
[micromatch]:https://github.com/micromatch/micromatch#options |
@@ -17,6 +17,12 @@ const vm = require('vm') | ||
const distDirPath = path.resolve(__dirname, 'dist') | ||
try { | ||
fs.unlink(distDir) | ||
} catch (e) {} | ||
describe('fs-directory', () => { | ||
it('compileDirectoryTo', () => { | ||
const baseDir = path.resolve(__dirname, './ts') | ||
const distDir = path.resolve(__dirname, './dist/directory') | ||
const distDir = path.resolve(distDirPath, './directory') | ||
@@ -28,3 +34,3 @@ fibTypify.compileDirectoryTo(baseDir, distDir) | ||
const baseDir = path.resolve(__dirname, './ts') | ||
const distDir = path.resolve(__dirname, './dist/directory1') | ||
const distDir = path.resolve(distDirPath, './directory1') | ||
@@ -36,6 +42,57 @@ fibTypify.compileDirectoryTo(baseDir, distDir, { compilerOptions: compilerOptions1 }) | ||
const baseDir = path.resolve(__dirname, './ts') | ||
const distDir = path.resolve(__dirname, './dist/directory2') | ||
const distDir = path.resolve(distDirPath, './directory2') | ||
fibTypify.compileDirectoryTo(baseDir, distDir, { compilerOptions: compilerOptions2 }) | ||
}) | ||
const baseDir = path.resolve(__dirname, './dir_to_copy') | ||
it('compileDirectoryTo with other files: default_copy', () => { | ||
const defaultCopyDir = path.resolve(distDirPath, './default_copy') | ||
fibTypify.compileDirectoryTo(baseDir, defaultCopyDir) | ||
assert.equal( fs.exists(path.resolve(defaultCopyDir, '1.txt')), false ) | ||
assert.equal( fs.exists(path.resolve(defaultCopyDir, '2.json')), true ) | ||
assert.equal( fs.exists(path.resolve(defaultCopyDir, '3.makefile')), false ) | ||
assert.equal( fs.exists(path.resolve(defaultCopyDir, '4.cpp')), false ) | ||
assert.equal( fs.exists(path.resolve(defaultCopyDir, 'glob_to_include')), true ) | ||
assert.equal( fs.exists(path.resolve(defaultCopyDir, 'glob_to_exclude123')), false ) | ||
}) | ||
it('compileDirectoryTo with other files: allcopystrategy_copy', () => { | ||
const allCopyStrategyDir = path.resolve(distDirPath, './allcopystrategy_copy') | ||
fibTypify.compileDirectoryTo(baseDir, allCopyStrategyDir, { | ||
extsToCopy: '*' | ||
}) | ||
assert.equal( fs.exists(path.resolve(allCopyStrategyDir, '1.txt')), true ) | ||
assert.equal( fs.exists(path.resolve(allCopyStrategyDir, '2.json')), true ) | ||
assert.equal( fs.exists(path.resolve(allCopyStrategyDir, '3.makefile')), true ) | ||
assert.equal( fs.exists(path.resolve(allCopyStrategyDir, '4.cpp')), true ) | ||
assert.equal( fs.exists(path.resolve(allCopyStrategyDir, 'node_modules')), false ) | ||
assert.equal( fs.exists(path.resolve(allCopyStrategyDir, 'glob_to_include')), true ) | ||
assert.equal( fs.exists(path.resolve(allCopyStrategyDir, 'glob_to_exclude123')), true ) | ||
}) | ||
it('compileDirectoryTo with other files: customized_copy', () => { | ||
const customizedCopyDir = path.resolve(distDirPath, './customized_copy') | ||
assert.throws(() => { | ||
fibTypify.compileDirectoryTo(baseDir, customizedCopyDir, { | ||
extsToCopy: ['.txt', 'cpp', 'makefile'] | ||
}) | ||
}) | ||
fibTypify.compileDirectoryTo(baseDir, customizedCopyDir, { | ||
extsToCopy: ['.txt'], | ||
includeLeveledGlobs: ['*', '!node_modules', 'glob_to_include', '!glob_to_exclude*'] | ||
}) | ||
assert.equal( fs.exists(path.resolve(customizedCopyDir, '1.txt')), true ) | ||
assert.equal( fs.exists(path.resolve(customizedCopyDir, '2.json')), false ) | ||
assert.equal( fs.exists(path.resolve(customizedCopyDir, '3.makefile')), false ) | ||
assert.equal( fs.exists(path.resolve(customizedCopyDir, '4.cpp')), false ) | ||
assert.equal( fs.exists(path.resolve(customizedCopyDir, 'node_modules')), false ) | ||
assert.equal( fs.exists(path.resolve(customizedCopyDir, 'glob_to_include')), true ) | ||
assert.equal( fs.exists(path.resolve(customizedCopyDir, 'glob_to_exclude123')), false ) | ||
}) | ||
}) |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
58631
29
467
104
3
2
+ Addedmicromatch@^3.1.10
+ Addedarr-diff@4.0.0(transitive)
+ Addedarr-flatten@1.1.0(transitive)
+ Addedarr-union@3.1.0(transitive)
+ Addedarray-unique@0.3.2(transitive)
+ Addedassign-symbols@1.0.0(transitive)
+ Addedatob@2.1.2(transitive)
+ Addedbase@0.11.2(transitive)
+ Addedbraces@2.3.2(transitive)
+ Addedcache-base@1.0.1(transitive)
+ Addedclass-utils@0.3.6(transitive)
+ Addedcollection-visit@1.0.0(transitive)
+ Addedcomponent-emitter@1.3.1(transitive)
+ Addedcopy-descriptor@0.1.1(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddecode-uri-component@0.2.2(transitive)
+ Addeddefine-property@0.2.51.0.02.0.2(transitive)
+ Addedexpand-brackets@2.1.4(transitive)
+ Addedextend-shallow@2.0.13.0.2(transitive)
+ Addedextglob@2.0.4(transitive)
+ Addedfill-range@4.0.0(transitive)
+ Addedfor-in@1.0.2(transitive)
+ Addedfragment-cache@0.2.1(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-value@2.0.6(transitive)
+ Addedhas-value@0.3.11.0.0(transitive)
+ Addedhas-values@0.1.41.0.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-accessor-descriptor@1.0.1(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedis-data-descriptor@1.0.1(transitive)
+ Addedis-descriptor@0.1.71.0.3(transitive)
+ Addedis-extendable@0.1.11.0.1(transitive)
+ Addedis-number@3.0.0(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedis-windows@1.0.2(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisobject@2.1.03.0.1(transitive)
+ Addedkind-of@3.2.24.0.06.0.3(transitive)
+ Addedmap-cache@0.2.2(transitive)
+ Addedmap-visit@1.0.0(transitive)
+ Addedmicromatch@3.1.10(transitive)
+ Addedmixin-deep@1.3.2(transitive)
+ Addedms@2.0.0(transitive)
+ Addednanomatch@1.2.13(transitive)
+ Addedobject-copy@0.1.0(transitive)
+ Addedobject-visit@1.0.1(transitive)
+ Addedobject.pick@1.3.0(transitive)
+ Addedpascalcase@0.1.1(transitive)
+ Addedposix-character-classes@0.1.1(transitive)
+ Addedregex-not@1.0.2(transitive)
+ Addedrepeat-element@1.1.4(transitive)
+ Addedrepeat-string@1.6.1(transitive)
+ Addedresolve-url@0.2.1(transitive)
+ Addedret@0.1.15(transitive)
+ Addedsafe-regex@1.1.0(transitive)
+ Addedset-value@2.0.1(transitive)
+ Addedsnapdragon@0.8.2(transitive)
+ Addedsnapdragon-node@2.1.1(transitive)
+ Addedsnapdragon-util@3.0.1(transitive)
+ Addedsource-map@0.5.7(transitive)
+ Addedsource-map-resolve@0.5.3(transitive)
+ Addedsource-map-url@0.4.1(transitive)
+ Addedsplit-string@3.1.0(transitive)
+ Addedstatic-extend@0.1.2(transitive)
+ Addedto-object-path@0.3.0(transitive)
+ Addedto-regex@3.0.2(transitive)
+ Addedto-regex-range@2.1.1(transitive)
+ Addedunion-value@1.0.1(transitive)
+ Addedunset-value@1.0.0(transitive)
+ Addedurix@0.1.0(transitive)
+ Addeduse@3.1.1(transitive)
Updatedtypescript@^2.9.1