New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fib-typify

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fib-typify - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

lib/_utils.js

77

lib/fs-directory.js
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 @@

1

lib/fs-file.js
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 )
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc