Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = async () => {
var normalizedPath = path.join(__dirname, '../modules');
const files = (await fs.readdir(normalizedPath))
.filter(fileName => !fileName.startsWith('.'))
.map(fileName => `${normalizedPath}/${fileName}`)
.filter(fileName => fileName.includes('additional'));
for (let file of files) {
const isDir = (await fs.lstat(file)).isDirectory();
// if (!isDir) {
try {
const moduleFile = require(file);
handleModuleFile(moduleFile);
} catch (e) {
console.log('unable to init', file, e);
}
// }
}
};
const resolveEntry = async (dir, entry) => {
const fpath = path.join(dir, entry)
// Might glob paths like `/foo/bar/chart.js`, which is a directory actually.
if (!(await lstat(fpath)).isFile()) return
try {
await this.resolveComponent(entry.replace(rExt, ''), { fpath, map: this.tree[pkg.name] })
} catch (err) {
if (err instanceof ParseError) {
console.warn(`WARN ${err.message}`, pkg.name, this.root)
} else {
throw err
}
}
}
async readComponent(id, isMain) {
const { cache, paths, root, system } = this
let [, name, version, entry] = id.match(rModuleId)
// Disallow access of components without version like `${system.name}/${entry}` because it is error prone. #36
if (!version) return
if (!(name in system.modules)) {
name = system.name
entry = id
}
const [fpath] = await findAsset(entry, paths, ['', '/index.js'])
if (!fpath) return
const stats = await lstat(fpath)
let { code } = await transformScript(id, {
cache, root, dir: root, fpath,
enableEnvify: true,
enableTransform: true
})
const dependencies = matchRequire.findAll(code)
code = define(id.replace(rExt, ''), dependencies, code)
code = isMain
? await this.formatMain(id, code)
: [code, `//# sourceMappingURL=./${path.basename(id)}.map`].join('\n')
return [code, {
'Last-Modified': stats.mtime.toJSON()
}]
}
if (!map) return
const { dir, browserify } = map
const fpath = path.join(dir, entry)
if (!fpath) return
if (!cacheExcept.includes('*') || cacheExcept.includes(name)) {
this.cacheModule({ name, version, entry }).catch(err => console.error(err.stack))
}
const { code } = await transformScript(id, {
cache, dir, fpath, root,
enableEnvify: browserify && browserify.transform && browserify.transform.includes('loose-envify'),
enableTransform: transformModuleNames.includes(name)
})
const stats = await lstat(fpath)
const dependencies = matchRequire.findAll(code)
return [define(id.replace(rExt, ''), dependencies, code), {
'Last-Modified': stats.mtime.toJSON()
}]
}
else if (id === 'porter-sw.js') {
result = await this.readSystemScript(id)
}
else if (await this.isSource(id)) {
result = await this.readSource(id)
}
else if (ext === '.js') {
result = await this.readScript(id, isMain)
}
else if (ext === '.css') {
result = await this.readStyle(id, isMain)
}
else if (rExt.test(ext)) {
const [fpath] = await findAsset(id, this.paths)
if (fpath) {
const [content, stats] = await Promise.all([readFile(fpath), lstat(fpath)])
result = [content, {
'Last-Modified': stats.mtime.toJSON()
}]
}
}
if (result) {
Object.assign(result[1], {
'Cache-Control': 'max-age=0',
'Content-Type': mime.lookup(ext),
ETag: crypto.createHash('md5').update(result[0]).digest('hex')
})
}
return result
}
async readSource(id) {
const fpath = path.join(this.root, id)
if (await exists(fpath)) {
const [content, stats] = await Promise.all([readFile(fpath, 'utf8'), lstat(fpath)])
return [content, {
'Last-Modified': stats.mtime.toJSON()
}]
}
}
async readSystemScript(id) {
if (!['loader.js', 'porter-sw.js'].includes(id)) {
throw new Error(`Unable to read '${id}' as system script`)
}
if ((inProduction || !debug.enabled) && id in this.systemScriptCache) {
return this.systemScriptCache[id]
}
const fpath = path.join(__dirname, '..', id)
const [content, stats] = await Promise.all([
readFile(fpath, 'utf8'),
lstat(fpath)
])
const result = [await envify(fpath, content), { 'Last-Modified': stats.mtime.toJSON() }]
this.systemScriptCache[id] = result
return result
}
const error = 'rootPath is not defined.'
logError(logger, error)
throw new Error(error)
}
if(!schemaPath) {
const error = 'schemaPath is not defined.'
logError(logger, error)
throw new Error(error)
}
if(!await fs.exists(schemaPath)) {
const error = 'schemaPath file does not exist.'
logError(logger, error)
throw new Error(error)
}
const progressCallback = (progress) ? throttle(progress, 16, {leading: true, trailing: true}) : () => {}
const fileStats = await fs.lstat(rootPath)
if (fileStats.isFile()) {
const result = await validateFile({
filePath: rootPath,
schemaPath,
logger
})
progressCallback(1, 1, rootPath)
return result
} else {
const fileNames = []
const readFiles = async (dir) => {
try {
const files = await fs.readdir(dir)
for(const file of files) {
const filePath = path.resolve(dir, file)
if (file.match(/\.json$/) && filePath !== schemaPath) {