tsdk
Version:
Type-safe API development and code share tool for TypeScript projects.
120 lines (119 loc) • 5.64 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformImportPath = exports.processImportPath = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const alias_1 = require("./alias");
const config_1 = require("./config");
const symbols_1 = __importDefault(require("./symbols"));
const transform_typeorm_entity_1 = require("./transform-typeorm-entity");
/** Handling import path */
function processImportPath(_importString, _filePath) {
let importString = _importString;
const commentString = importString.slice(0, 2);
const hasComment = commentString === '//' || commentString === '/*';
const arr = _filePath.split('/');
arr.pop();
const filePath = arr.join('/');
const isDoubleSemicolon = importString.indexOf('from "') > -1;
const matched = importString.match(isDoubleSemicolon ? /from "(.*)";/ : /from '(.*)';/);
if (matched) {
// path alias check and replace path
const fromPath = (0, alias_1.aliasToRelativePath)({
cwd: './',
config: config_1.tsconfig,
imports: [matched[1]],
filePath: _filePath,
})[0];
importString = _importString.replace(matched[1], fromPath);
const firstLevelPath = fromPath.split('/')[0];
if (firstLevelPath[0] !== '.') {
const isShareLib = !!(0, config_1.getDeps)()[firstLevelPath];
if (isShareLib) {
return importString;
}
else {
if (!hasComment) {
console.warn(symbols_1.default.space, symbols_1.default.warning, `Warn: '${firstLevelPath}' not support. If you confirm '${firstLevelPath}' will use in the both side, please add this lib to the '${config_1.ensureDir}/package.json' dependencies`);
}
}
}
const finalPath = path_1.default.join(filePath, fromPath);
const isEntityOrApiconf = importString.indexOf(`.${config_1.config.entityExt}`) > -1 ||
importString.indexOf(`.${config_1.config.apiconfExt}`) > -1 ||
importString.indexOf(`.${config_1.config.shareExt}`) > -1;
// if (isEntityOrApiconf) {
// console.log(_importString);
// }
if (!hasComment) {
const findDir = isEntityOrApiconf ||
config_1.config.sharedDirs.find((dir) => {
const currentShareDir = path_1.default.normalize(dir);
return finalPath.indexOf(currentShareDir) === 0;
});
if (!findDir) {
console.log(symbols_1.default.space, symbols_1.default.error, `Error: Don't import file from outside of shared dirs: '${importString}'`, _filePath);
}
}
}
else {
console.warn(symbols_1.default.space, symbols_1.default.warning, `No match: ${importString}`);
}
return importString;
}
exports.processImportPath = processImportPath;
/** parse import alias path and transform */
function transformImportPath(filePath, isEntity) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
let res = yield fs_extra_1.default.readFile(filePath, 'utf-8');
if (isEntity) {
res = ((_a = config_1.config.entityLibName) === null || _a === void 0 ? void 0 : _a.includes('typeorm')) ? (0, transform_typeorm_entity_1.transformTypeormEntity)(res, 'typeorm') : res;
res = res.includes('@nestjs') ? (0, transform_typeorm_entity_1.transformTypeormEntity)(res, '@nestjs') : res;
}
const result = res.split('\n');
const imports = [];
const otherContent = [];
let importArr = [];
result.forEach((i) => {
const inlineImport = i.indexOf("import '") > -1 || i.indexOf('import "') > -1;
const hasImport = i.indexOf('import ') > -1;
const hasFrom = i.indexOf(' from "') > -1 || i.indexOf(" from '") > -1;
if (inlineImport) {
imports.push(i);
}
else if (hasImport && hasFrom) {
imports.push(processImportPath(i, filePath));
}
else if (hasImport) {
importArr.push(i);
}
else if (hasFrom) {
importArr.push(i);
imports.push(processImportPath(importArr.join(''), filePath));
importArr = [];
}
else if (importArr.length > 0) {
importArr.push(i);
}
else {
otherContent.push(i);
}
});
const fileContent = imports.join('\n') + otherContent.join('\n');
return fileContent;
});
}
exports.transformImportPath = transformImportPath;