mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 18:12:09 +08:00
refactor: simplify project file lookup
This commit is contained in:
@ -1,11 +1,10 @@
|
|||||||
import Config from 'webpack-chain';
|
import Config from 'webpack-chain';
|
||||||
import svelte from '../../src/configuration/svelte';
|
import svelte from '../../src/configuration/svelte';
|
||||||
import { init } from '../../src';
|
import { init } from '../../src';
|
||||||
import { mockFile } from '../../scripts/jest.mockFiles';
|
|
||||||
|
|
||||||
mockFile('./svelte.config.js', '');
|
jest.mock('__jest__/svelte.config.js', () => {
|
||||||
// jest.mock('__jest__/svelte.config.js', () => {
|
|
||||||
// }, { virtual: true })
|
}, { virtual: true })
|
||||||
|
|
||||||
describe('svelte configuration', () => {
|
describe('svelte configuration', () => {
|
||||||
const platforms = ['ios', 'android'];
|
const platforms = ['ios', 'android'];
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
},
|
},
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc --project tsconfig.build.json && ts-node ./scripts/injectGlobals.ts dist/index.js __TEST__=false",
|
"build": "tsc --project tsconfig.build.json",
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"prepack": "npm run build && cp -R src/stubs dist/stubs && chmod +x dist/bin/index.js"
|
"prepack": "npm run build && cp -R src/stubs dist/stubs && chmod +x dist/bin/index.js"
|
||||||
},
|
},
|
||||||
@ -65,7 +65,6 @@
|
|||||||
"memfs": "^3.2.0",
|
"memfs": "^3.2.0",
|
||||||
"nativescript-vue-template-compiler": "^2.8.2",
|
"nativescript-vue-template-compiler": "^2.8.2",
|
||||||
"ts-jest": "^26.5.2",
|
"ts-jest": "^26.5.2",
|
||||||
"ts-node": "^9.1.1",
|
|
||||||
"typescript": "^4.2.2",
|
"typescript": "^4.2.2",
|
||||||
"unionfs": "^4.4.0"
|
"unionfs": "^4.4.0"
|
||||||
},
|
},
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
import { readFileSync, writeFileSync, existsSync } from 'fs';
|
|
||||||
import { program } from 'commander';
|
|
||||||
import { resolve } from 'path';
|
|
||||||
|
|
||||||
program
|
|
||||||
.description('Inject globals into a dist file')
|
|
||||||
.arguments('<target> <globals...>')
|
|
||||||
.parse(process.argv);
|
|
||||||
|
|
||||||
const target = resolve(program.args[0]);
|
|
||||||
|
|
||||||
if (!existsSync(target)) {
|
|
||||||
console.log(`Invalid target: ${program.args[0]}. ${target} does not exist.`);
|
|
||||||
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const globals = program.args.slice(1);
|
|
||||||
const toInject = globals.map((global) => {
|
|
||||||
const [name, value] = global.split('=');
|
|
||||||
return `global.${name} = ${value};`;
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(`Injecting to ${target}:`);
|
|
||||||
console.log(toInject.join('\n'));
|
|
||||||
|
|
||||||
let fileContents = readFileSync(target).toString();
|
|
||||||
fileContents = `${fileContents}\n${toInject.join('\n')}`;
|
|
||||||
|
|
||||||
writeFileSync(target, fileContents);
|
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env node
|
#!/bin/env node
|
||||||
|
|
||||||
import { redBright, green, greenBright } from 'chalk';
|
import { redBright, green, greenBright } from 'chalk';
|
||||||
import { program } from 'commander';
|
import { program } from 'commander';
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import Config from 'webpack-chain';
|
import Config from 'webpack-chain';
|
||||||
|
|
||||||
import { getProjectRootPath } from '../helpers/project';
|
import { getProjectFilePath, getProjectRootPath } from '../helpers/project';
|
||||||
import { getPlatformName } from '../helpers/platform';
|
import { getPlatformName } from '../helpers/platform';
|
||||||
import { env as _env, IWebpackEnv } from '../index';
|
import { env as _env, IWebpackEnv } from '../index';
|
||||||
import { error } from '../helpers/log';
|
import { error } from '../helpers/log';
|
||||||
@ -52,15 +52,16 @@ function getSvelteConfigPreprocessor(): any {
|
|||||||
return config?.preprocess;
|
return config?.preprocess;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSvelteConfig(): { preprocess: any } | undefined {
|
interface ISvelteConfig {
|
||||||
|
preprocess: any
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSvelteConfig(): ISvelteConfig | undefined {
|
||||||
try {
|
try {
|
||||||
const resolvedPath = require.resolve(`./svelte.config.js`, {
|
return require(
|
||||||
paths: [getProjectRootPath()],
|
getProjectFilePath('svelte.config.js')
|
||||||
});
|
) as ISvelteConfig;
|
||||||
return require(resolvedPath);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// todo: remove when jest supports mocking require.resolve
|
|
||||||
if (__TEST__) return;
|
|
||||||
error('Could not find svelte.config.js.', err);
|
error('Could not find svelte.config.js.', err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
packages/webpack5/src/globals.d.ts
vendored
2
packages/webpack5/src/globals.d.ts
vendored
@ -1 +1 @@
|
|||||||
declare var __TEST__: boolean;
|
// define globals here
|
||||||
|
@ -19,7 +19,15 @@ interface IPackageJson {
|
|||||||
* Utility function to get the contents of the project package.json
|
* Utility function to get the contents of the project package.json
|
||||||
*/
|
*/
|
||||||
export function getPackageJson() {
|
export function getPackageJson() {
|
||||||
const packageJsonPath = resolve(getProjectRootPath(), 'package.json');
|
return require(
|
||||||
|
getProjectFilePath('package.json')
|
||||||
return require(packageJsonPath) as IPackageJson;
|
) as IPackageJson;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility to get project files relative to the project root.
|
||||||
|
* @param filePath path to get
|
||||||
|
*/
|
||||||
|
export function getProjectFilePath(filePath: string): string {
|
||||||
|
return resolve(getProjectRootPath(), filePath);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user