Skip to content

Instantly share code, notes, and snippets.

@bajtos
Last active May 15, 2020 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bajtos/68672a8229bd03fddad5a4ad1442061f to your computer and use it in GitHub Desktop.
Save bajtos/68672a8229bd03fddad5a4ad1442061f to your computer and use it in GitHub Desktop.
Rework loopback-next infrastructure to remove top-level index files
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: loopback-next
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const path = require('path');
const fs = require('fs-extra');
if (require.main === module) {
fixIndexFiles().then(
ok => process.exit(ok ? 0 : 1),
err => {
console.error(err);
process.exit(2);
},
);
}
async function fixIndexFiles() {
const Project = require('@lerna/project');
const project = new Project(process.cwd());
const packages = await project.getPackages();
for (const p of packages) {
await fixPackage(p, project.rootPath);
}
}
async function fixPackage(p, rootPath) {
// Skip JS-only projects like CLI
const isTsProject = await fs.pathExists(
path.join(p.location, 'tsconfig.json'),
);
if (!isTsProject) return;
// 1. Remove top-level index files
for (const i of ['index.js', 'index.ts', 'index.d.ts']) {
await fs.remove(path.join(p.location, i));
}
const pkgJsonFile = path.join(p.location, 'package.json');
const meta = await fs.readJson(pkgJsonFile);
// 2. Update entry points
// We want `main` and `types` to be among the first fields,
// therefore we have to re-populate the object
const newMeta = {
name: meta.name,
version: meta.version,
description: meta.description,
private: meta.private,
main: undefined,
types: undefined,
...meta,
};
newMeta.main = 'dist/index.js';
newMeta.types = 'dist/index.d.ts';
if (!newMeta.repository) {
newMeta.repository = {
type: 'git',
url: 'https://github.com/strongloop/loopback-next.git',
directory: path.relative(rootPath, p.location),
};
}
// 3. Update package contents
// Note that example packages don't have `files` section, the npm package
// must include ALL files
if (newMeta.files) {
newMeta.files = newMeta.files.filter(f => !f.startsWith('index.'));
}
await fs.writeJson(pkgJsonFile, newMeta, {spaces: 2});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment