Skip to content

Commit

Permalink
feat: aliases support, cleanup
Browse files Browse the repository at this point in the history
fix: paths when using `npm link`
  • Loading branch information
serrg authored and JulianWielga committed Jul 21, 2021
1 parent 99b4885 commit 064eb35
Show file tree
Hide file tree
Showing 3 changed files with 739 additions and 501 deletions.
48 changes: 35 additions & 13 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const path = require('path');
const fs = require('fs');
const os = require("os");
const findNodeModules = require('find-node-modules');
const ts = require('typescript');

const formatHost = {
Expand All @@ -14,26 +14,29 @@ function reportDiagnostic(diagnostic) {
console.log("TS Error", diagnostic.code, ":", ts.flattenDiagnosticMessageText( diagnostic.messageText, formatHost.getNewLine()));
}

const [nodeModules] = findNodeModules({ cwd: process.argv[1], relative: false });

const getArg = (argName) => {
const argIndex = process.argv.indexOf(argName);
return argIndex !== -1 ? process.argv[argIndex + 1] : null;
};

const outDirArg = getArg('--outputDir');

const outputDir = outDirArg
? path.resolve('./', outDirArg)
: path.resolve(__dirname, '../../@types/__federated_types/');
: path.resolve(nodeModules, '@types/__federated_types/');

const findFederationConfig = (base) => {
let files = fs.readdirSync(base);
let queue = [];

for( let i = 0; i < files.length; i++ ) {
for (let i = 0; i < files.length; i++) {
const file = files[i];
const newBase = path.join(base, file);
if (file === 'federation.config.json') {
return path.resolve('./', newBase);
} else if(fs.statSync(newBase).isDirectory() && !newBase.includes('node_modules')) {
} else if (fs.statSync(newBase).isDirectory() && !newBase.includes('node_modules')) {
queue.push(newBase);
}
}
Expand All @@ -54,8 +57,14 @@ console.log(`Using config file: ${federationConfigPath}`);

const federationConfig = require(federationConfigPath);
const compileFiles = Object.values(federationConfig.exposes);
const compileKeys = Object.keys(federationConfig.exposes);
const outFile = path.resolve(outputDir, `${federationConfig.name}.d.ts`);

function getModuleDeclareName(exposeName) {
// windows paths 🤦
return path.join(federationConfig.name, exposeName).replace(/[\\/]/g, '/');
}

try {
if (fs.existsSync(outFile)) {
fs.unlinkSync(outFile);
Expand Down Expand Up @@ -89,28 +98,41 @@ try {
}

moduleNames.forEach((name) => {
const regex = RegExp(`"${name}`, 'g');
typing = typing.replace(regex, `"${federationConfig.name}/${name}`);
// exposeName - relative name of exposed component (if not found - just take moduleName)
const [exposeName = name, ...aliases] = compileKeys.filter(key => federationConfig.exposes[key].endsWith(name));
const regex = RegExp(`"${name}"`, 'g');

const moduleDeclareName = getModuleDeclareName(exposeName);

// language=TypeScript
const createAliasModule = name => `
declare module "${getModuleDeclareName(name)}" {
export * from "${moduleDeclareName}"
}
`;

typing = [
typing.replace(regex, `"${moduleDeclareName}"`),
...aliases.map(createAliasModule),
].join('\n');
});

console.log('writing typing file:', outFile);

fs.writeFileSync(outFile, typing);

// if we are writing to the node_modules/@types directory, add a package.json file
if (outputDir.includes( os.platform() === "win32"
? "node_modules\\@types"
: "node_modules/@types")) {
if (outputDir.includes(path.join('node_modules', '@types'))) {
const packageJsonPath = path.resolve(outputDir, 'package.json');

if (!fs.existsSync(packageJsonPath)) {
console.log('writing package.json:', packageJsonPath);
console.debug('writing package.json:', packageJsonPath);
fs.copyFileSync(path.resolve(__dirname, 'typings.package.tmpl.json'), packageJsonPath);
} else {
console.log(packageJsonPath, 'already exists');
console.debug(packageJsonPath, 'already exists');
}
} else {
console.log('not writing to node modules, dont need a package.json');
console.debug('not writing to node modules, dont need a package.json');
}

// write/update the index.d.ts file
Expand All @@ -128,7 +150,7 @@ try {
}
}

console.log('Success!');
console.debug('Success!');
} catch (e) {
console.error(`ERROR:`, e);
process.exit(1);
Expand Down
Loading

0 comments on commit 064eb35

Please sign in to comment.