Merge pull request #484 from spotify/rugvip/relatives

packages/cli: make src-relative imports work
This commit is contained in:
Patrik Oldsberg
2020-04-07 11:05:08 +02:00
committed by GitHub
19 changed files with 106 additions and 29 deletions
+48
View File
@@ -5,12 +5,60 @@ const path = require('path');
// Figure out whether we're running inside the backstage repo or as an installed dependency
const isLocal = require('fs').existsSync(path.resolve(__dirname, '../src'));
if (!isLocal) {
// src-relative imports are a pain to get to work with plain tsc compilation, as the
// transpiled code will maintain the imports as they are in the source. Which means an
// import for `helpers/paths` will start like that in the output, which won't work in NodeJS.
//
// This is a solution for getting src-relative imports to work with typescript/node in
// the published package. We're using `module: "amd"` to ship the entire cli implementation
// in one file, and it also happens to generate correct module definition and import statements.
// Minimal AMD implementation
const moduleFactories = {};
const moduleCache = {};
global.define = (name, deps, moduleFunc) => {
moduleFactories[name] = () => {
const exportsObj = {};
const impls = deps.slice(2).map(dep => {
const factory = moduleFactories[dep];
if (!factory) {
return require(dep);
}
if (!moduleCache[dep]) {
moduleCache[dep] = factory();
}
return moduleCache[dep];
});
moduleFunc(require, exportsObj, ...impls);
return exportsObj;
};
};
require('../dist');
// index module is the entrypoint
moduleFactories.index();
} else {
const tsConfigPath = path.resolve(__dirname, '../tsconfig.json');
const {
baseUrl: relativeBaseUrl,
paths = {},
} = require(tsConfigPath).compilerOptions;
const baseUrl = path.resolve(__dirname, '..', relativeBaseUrl);
// Use tsconfig-paths to resolve src-relative paths during development
const cleanupPaths = require('tsconfig-paths').register({ baseUrl, paths });
require('ts-node').register({
project: path.resolve(__dirname, '../tsconfig.json'),
compilerOptions: {
module: 'CommonJS',
},
transpileOnly: true,
});
require('../src');
cleanupPaths();
}
+3 -1
View File
@@ -24,7 +24,9 @@ if (fs.existsSync('jest.config.js')) {
} else if (fs.existsSync('jest.config.ts')) {
module.exports = require(path.resolve('jest.config.ts'));
} else {
const extraOptions = {};
const extraOptions = {
modulePaths: ['<rootDir>'],
};
// Use src/setupTests.ts as the default location for configuring test env
if (fs.existsSync('src/setupTests.ts')) {
+2 -1
View File
@@ -39,7 +39,8 @@
"@types/webpack-dev-server": "^3.10.0",
"del": "^5.1.0",
"nodemon": "^2.0.2",
"ts-node": "^8.6.2"
"ts-node": "^8.6.2",
"tsconfig-paths": "^3.9.0"
},
"bin": {
"backstage-cli": "bin/backstage-cli"
+1 -1
View File
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { run } from '../../helpers/run';
import { run } from 'helpers/run';
export default async () => {
const args = ['build'];
+2 -2
View File
@@ -14,8 +14,8 @@
* limitations under the License.
*/
import { run } from '../../helpers/run';
import { watchDeps } from '../watch-deps';
import { run } from 'helpers/run';
import { watchDeps } from 'commands/watch-deps';
export default async () => {
const args = ['start'];
@@ -16,10 +16,10 @@
import fs from 'fs-extra';
import { resolve as resolvePath, relative as relativePath } from 'path';
import { runPlain, runCheck } from '../../helpers/run';
import { runPlain, runCheck } from 'helpers/run';
import { Options } from './options';
import { extractArchive, createArchive } from './archive';
import { paths } from '../../helpers/paths';
import { paths } from 'helpers/paths';
const INFO_FILE = '.backstage-build-cache';
@@ -15,7 +15,7 @@
*/
import { Command } from 'commander';
import { run } from '../../helpers/run';
import { run } from 'helpers/run';
import { Cache } from './cache';
import { parseOptions } from './options';
@@ -16,7 +16,7 @@
import { resolve as resolvePath } from 'path';
import { Command } from 'commander';
import { paths } from '../../helpers/paths';
import { paths } from 'helpers/paths';
const DEFAULT_MAX_ENTRIES = 10;
@@ -21,8 +21,8 @@ import inquirer, { Answers, Question } from 'inquirer';
import { exec as execCb } from 'child_process';
import { resolve as resolvePath } from 'path';
import os from 'os';
import { Task, templatingTask } from '../../helpers/tasks';
import { paths } from '../../helpers/paths';
import { Task, templatingTask } from 'helpers/tasks';
import { paths } from 'helpers/paths';
const exec = promisify(execCb);
async function checkExists(rootDir: string, name: string) {
@@ -26,8 +26,8 @@ import {
addCodeownersEntry,
getCodeownersFilePath,
} from './lib/codeowners';
import { paths } from '../../helpers/paths';
import { Task, templatingTask } from '../../helpers/tasks';
import { paths } from 'helpers/paths';
import { Task, templatingTask } from 'helpers/tasks';
const exec = promisify(execCb);
async function checkExists(rootDir: string, id: string) {
+1 -1
View File
@@ -15,7 +15,7 @@
*/
import { Command } from 'commander';
import { run } from '../helpers/run';
import { run } from 'helpers/run';
export default async (cmd: Command) => {
const args = ['lint', '--max-warnings=0', '--format=codeframe'];
@@ -22,7 +22,7 @@ import postcss from 'rollup-plugin-postcss';
import imageFiles from 'rollup-plugin-image-files';
import json from '@rollup/plugin-json';
import { RollupWatchOptions } from 'rollup';
import { paths } from '../../helpers/paths';
import { paths } from 'helpers/paths';
export default {
input: 'src/index.ts',
@@ -15,7 +15,7 @@
*/
import { Command } from 'commander';
import { run } from '../../helpers/run';
import { run } from 'helpers/run';
export default async (cmd: Command) => {
const args = ['test'];
+2 -2
View File
@@ -15,8 +15,8 @@
*/
import { Command } from 'commander';
import { run } from '../helpers/run';
import { paths } from '../helpers/paths';
import { run } from 'helpers/run';
import { paths } from 'helpers/paths';
export default async (cmd: Command) => {
const args = ['test', '--config', paths.resolveOwn('config/jest.js')];
@@ -21,8 +21,8 @@ import { getPackageDeps } from './packages';
import { startWatcher, startPackageWatcher } from './watcher';
import { startCompiler } from './compiler';
import { startChild } from './child';
import { waitForExit } from '../../helpers/run';
import { paths } from '../../helpers/paths';
import { waitForExit } from 'helpers/run';
import { paths } from 'helpers/paths';
const PACKAGE_BLACKLIST = [
// We never want to watch for changes in the cli, but all packages will depend on it.
@@ -16,7 +16,7 @@
import fs from 'fs';
import { promisify } from 'util';
import { paths } from '../../helpers/paths';
import { paths } from 'helpers/paths';
const readFile = promisify(fs.readFile);
+15 -1
View File
@@ -77,8 +77,22 @@ export function findRootPath(topPath: string): string {
);
}
// Finds the root of the cli package itself
export function findOwnDir() {
// Known relative locations of package in dist/dev
const pathDist = '..';
const pathDev = '../..';
// Check the closest dir first
const pkgInDist = resolvePath(__dirname, pathDist, 'package.json');
const isDist = fs.pathExistsSync(pkgInDist);
const path = isDist ? pathDist : pathDev;
return resolvePath(__dirname, path);
}
export function findPaths(): Paths {
const ownDir = resolvePath(__dirname, '../..');
const ownDir = findOwnDir();
const targetDir = fs.realpathSync(process.cwd());
// We're not always running in a monorepo, so we lazy init this to only crash commands
+3 -6
View File
@@ -2,11 +2,8 @@
"extends": "../../tsconfig.json",
"include": ["src"],
"compilerOptions": {
"outDir": "dist",
"module": "CommonJS",
"baseUrl": "src",
"paths": {
"*": ["src/*"]
}
"outFile": "dist/index.js",
"module": "amd",
"baseUrl": "src"
}
}
+16 -1
View File
@@ -3657,6 +3657,11 @@
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339"
integrity sha512-8+KAKzEvSUdeo+kmqnKrqgeE+LcA0tjYWFY7RPProVYwnqDjukzO+3b6dLD56rYX5TdWejnEOLJYOIeh4CXKuA==
"@types/json5@^0.0.29":
version "0.0.29"
resolved "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=
"@types/mime@*":
version "2.0.1"
resolved "https://registry.npmjs.org/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d"
@@ -16919,7 +16924,7 @@ request@^2.85.0, request@^2.87.0, request@^2.88.0:
tunnel-agent "^0.6.0"
uuid "^3.3.2"
request@cypress-io/request#b5af0d1fa47eec97ba980cde90a13e69a2afcd16:
"request@github:cypress-io/request#b5af0d1fa47eec97ba980cde90a13e69a2afcd16":
version "2.88.1"
resolved "https://codeload.github.com/cypress-io/request/tar.gz/b5af0d1fa47eec97ba980cde90a13e69a2afcd16"
dependencies:
@@ -18987,6 +18992,16 @@ ts-pnp@1.1.6, ts-pnp@^1.1.2, ts-pnp@^1.1.6:
resolved "https://registry.npmjs.org/ts-pnp/-/ts-pnp-1.1.6.tgz#389a24396d425a0d3162e96d2b4638900fdc289a"
integrity sha512-CrG5GqAAzMT7144Cl+UIFP7mz/iIhiy+xQ6GGcnjTezhALT02uPMRw7tgDSESgB5MsfKt55+GPWw4ir1kVtMIQ==
tsconfig-paths@^3.9.0:
version "3.9.0"
resolved "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz#098547a6c4448807e8fcb8eae081064ee9a3c90b"
integrity sha512-dRcuzokWhajtZWkQsDVKbWyY+jgcLC5sqJhg2PSgf4ZkH2aHPvaOY8YWGhmjb68b5qqTfasSsDO9k7RUiEmZAw==
dependencies:
"@types/json5" "^0.0.29"
json5 "^1.0.1"
minimist "^1.2.0"
strip-bom "^3.0.0"
tslib@1.10.0:
version "1.10.0"
resolved "https://registry.npmjs.org/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"