sensai
Version:
Because even AI needs a master
140 lines (139 loc) • 5.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, /**
* Bundle (i.e compile, optimize) source directory into destination directory.
*
* @param options.cwdPath working directory
* @param options.destPath destination directory
* @param options.srcPath source directory
* @param options.copyFiles copy all files in srcPath if `true`
*
* @notes
*
* - TODO we should look at bundling routes if possible
* - TODO compile routes config
*/ "default", {
enumerable: true,
get: function() {
return _default;
}
});
const _utils = require("lib/utils");
const _options = /*#__PURE__*/ _interop_require_wildcard(require("./options"));
const _promises = require("node:fs/promises");
const _nodepath = require("node:path");
const _nodestream = require("node:stream");
const _core = require("@swc/core");
function _getRequireWildcardCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireWildcardCache = function(nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
return {
default: obj
};
}
var cache = _getRequireWildcardCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {
__proto__: null
};
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
for(var key in obj){
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}
const _default = async (cb, options)=>{
const stream = bundle(options);
return new Promise((resolve, reject)=>{
stream.on('data', cb);
stream.on('end', resolve);
stream.on('error', reject);
});
};
/**
* Read all files in a directory and transform files with supported
* extensions, copy otherwise.
* This method is a pass through and only push transformed files down the pipe.
*/ const bundle = (options)=>{
const { cwdPath, srcPath = cwdPath } = options;
const compilerOptions = (0, _options.getCompilerOptions)(cwdPath);
return (0, _utils.readApiDir)(srcPath, {
// TODO we should load all ignore from .gitignore
// on top of node modules and sensai
// TODO git ls-tree --full-tree --name-only -r HEAD # list all files in git
ignore: [
'node_modules',
'.sensai'
]
}).pipe(new _nodestream.Transform({
objectMode: true,
async transform (buffer, _encoding, callback) {
const path = await write(buffer.toString(), {
...options,
compilerOptions
});
callback(null, path);
}
}));
};
/**
* This method will check if a given file has an extension supported by our compiler
* and will transform the file if that's the case. The transformed file is then
* copied over `options.destPath` as a `cjs` file.
*/ const write = async (filePath, opts)=>{
const { compilerOptions, copyFiles, cwdPath, destPath } = opts;
const config = (0, _options.default)(filePath, compilerOptions);
if (config) {
const { dir, ext, name } = (0, _nodepath.parse)(filePath);
const path = (0, _nodepath.join)(destPath, (0, _nodepath.relative)(cwdPath, (0, _nodepath.join)(dir, name + ext)));
await copyTransformFile(filePath, path, async (content)=>{
const { code } = await (0, _core.transform)(content, config);
// TODO generate maps
return code;
});
return path;
} else if (copyFiles) {
const path = (0, _nodepath.join)(destPath, (0, _nodepath.relative)(cwdPath, filePath));
await copyTransformFile(filePath, path);
return path;
}
};
/**
* This method is used to copy one file from one location to an other and apply
* a transformation if specified.
* This method is used in production mode.
* @private
*/ const copyTransformFile = async (filePath, destPath, transform = (content)=>content)=>{
const file = await (0, _promises.readFile)(filePath);
await (0, _promises.mkdir)((0, _nodepath.dirname)(destPath), {
recursive: true
}) // TODO should we check if we have access?
;
const content = await transform(file.toString());
await (0, _promises.writeFile)(destPath, content);
};