@sprucelabs/babel-plugin-schema
Advanced tools
Comparing version 1.0.22 to 1.1.0
@@ -40,12 +40,5 @@ { | ||
}, | ||
"eslint.workingDirectories": [ | ||
"./src", | ||
], | ||
"eslint.workingDirectories": ["."], | ||
"eslint.options": { | ||
"extensions": [ | ||
".js", | ||
".jsx", | ||
".ts", | ||
".tsx" | ||
] | ||
"extensions": [".js", ".jsx", ".ts", ".tsx"] | ||
}, | ||
@@ -66,10 +59,6 @@ "typescript.tsdk": "node_modules/typescript/lib", | ||
], | ||
"cSpell.words": [ | ||
"arkit", | ||
"autogenerated", | ||
"spreedsheet" | ||
], | ||
"cSpell.words": ["arkit", "autogenerated", "spreedsheet"], | ||
"files.exclude": { | ||
"packages/*/build": true | ||
}, | ||
} | ||
} | ||
} |
import AbstractSpruceTest from '@sprucelabs/test'; | ||
export default class SchemaBuildsAndMapsPathsTest extends AbstractSpruceTest { | ||
protected static testDirsToDelete: string[]; | ||
protected static afterAll(): Promise<void>; | ||
protected static buildsSchemaWithoutError(): Promise<void>; | ||
private static fieldFactoryFilepath; | ||
protected static buildsSchemaAndUsesTheHashSpruceVersionOfFiles(): Promise<void>; | ||
private static copyDir; | ||
private static setupNewPackage; | ||
private static setupNewCwd; | ||
private static copyTsConfig; | ||
private static invokeYarnCommands; | ||
} |
@@ -33,10 +33,16 @@ "use strict"; | ||
const os_1 = __importDefault(require("os")); | ||
const path_1 = __importDefault(require("path")); | ||
const test_1 = __importStar(require("@sprucelabs/test")); | ||
const fs_extra_1 = __importDefault(require("fs-extra")); | ||
const rimraf_1 = __importDefault(require("rimraf")); | ||
const index_1 = require("../../index"); | ||
class SchemaBuildsAndMapsPathsTest extends test_1.default { | ||
static async afterAll() { | ||
super.afterAll(); | ||
for (const dir of this.testDirsToDelete) { | ||
rimraf_1.default.sync(dir); | ||
} | ||
} | ||
static async buildsSchemaWithoutError() { | ||
const cwd = await this.setupNewPackage(); | ||
const fieldFactoryFile = path_1.default.join(cwd, 'node_modules/@sprucelabs/schema', 'build/factories/FieldFactory.js'); | ||
const fieldFactoryFile = this.fieldFactoryFilepath(cwd); | ||
const contents = fs_extra_1.default.readFileSync(fieldFactoryFile).toString(); | ||
@@ -47,7 +53,53 @@ test_1.assert.doesInclude(contents, '#spruce'); | ||
test_1.assert.doesNotInclude(afterMapContents, '#spruce'); | ||
test_1.assert.doesInclude(afterMapContents, '@sprucelabs/schema/build/.spruce/'); | ||
} | ||
static fieldFactoryFilepath(cwd) { | ||
return this.resolvePath(cwd, 'node_modules/@sprucelabs/schema', 'build/factories/FieldFactory.js'); | ||
} | ||
static async buildsSchemaAndUsesTheHashSpruceVersionOfFiles() { | ||
const cwd = await this.setupNewPackage(); | ||
// copy schema files | ||
const sourceHashSpruce = this.resolvePath(cwd, 'node_modules', '@sprucelabs/schema', 'build', '.spruce'); | ||
const destinationHashSpruce = this.resolvePath(cwd, 'build', '.spruce'); | ||
await this.copyDir(sourceHashSpruce, destinationHashSpruce); | ||
// copy ts config | ||
const sourceTsConfig = this.resolvePath('src', '__tests__', 'files', 'test-tsconfig.json'); | ||
const tsConfigContents = fs_extra_1.default.readFileSync(sourceTsConfig); | ||
const destinationTsConfig = this.resolvePath(cwd, 'tsconfig.json'); | ||
fs_extra_1.default.writeFileSync(destinationTsConfig, tsConfigContents); | ||
index_1.copyAndMap({ cwd, destination: cwd }); | ||
const fieldFactoryFile = this.fieldFactoryFilepath(cwd); | ||
const afterMapContents = fs_extra_1.default.readFileSync(fieldFactoryFile).toString(); | ||
test_1.assert.doesNotInclude(afterMapContents, '#spruce'); | ||
test_1.assert.doesNotInclude(afterMapContents, '@sprucelabs/schema/build/.spruce/'); | ||
} | ||
static async copyDir(source, destination) { | ||
fs_extra_1.default.ensureDir(destination); | ||
return new Promise((resolve) => { | ||
child_process_1.exec(`cd ${source} && tar cf - . | (cd ${destination}; tar xf -)`, { maxBuffer: 1024 * 1024 * 5 }, (err, stdout) => { | ||
if (err) { | ||
throw err; | ||
} | ||
resolve(stdout); | ||
}); | ||
}); | ||
} | ||
static async setupNewPackage() { | ||
const cwd = await this.setupNewCwd(); | ||
this.testDirsToDelete.push(cwd); | ||
await this.invokeYarnCommands(cwd); | ||
this.copyTsConfig(cwd); | ||
return cwd; | ||
} | ||
static async setupNewCwd() { | ||
const today = new Date(); | ||
const cwd = path_1.default.join(os_1.default.tmpdir(), 'babel-plugin-schema', `${today.getTime()}`); | ||
const cwd = this.resolvePath(os_1.default.tmpdir(), 'babel-plugin-schema', `${today.getTime()}`); | ||
await fs_extra_1.default.ensureDir(cwd); | ||
return cwd; | ||
} | ||
static copyTsConfig(cwd) { | ||
const tsConfigContents = fs_extra_1.default.readFileSync(this.resolvePath(__dirname, '..', '..', '..', 'tsconfig.json')); | ||
fs_extra_1.default.writeFileSync(this.resolvePath(cwd, 'tsconfig.json'), tsConfigContents); | ||
} | ||
static async invokeYarnCommands(cwd) { | ||
await new Promise((resolve, reject) => { | ||
@@ -68,11 +120,12 @@ child_process_1.exec(`yarn init --yes && yarn add @sprucelabs/schema`, { | ||
}); | ||
const tsConfigContents = fs_extra_1.default.readFileSync(path_1.default.join(__dirname, '..', '..', '..', 'tsconfig.json')); | ||
fs_extra_1.default.writeFileSync(path_1.default.join(cwd, 'tsconfig.json'), tsConfigContents); | ||
return cwd; | ||
} | ||
} | ||
SchemaBuildsAndMapsPathsTest.testDirsToDelete = []; | ||
__decorate([ | ||
test_1.test() | ||
], SchemaBuildsAndMapsPathsTest, "buildsSchemaWithoutError", null); | ||
__decorate([ | ||
test_1.test() | ||
], SchemaBuildsAndMapsPathsTest, "buildsSchemaAndUsesTheHashSpruceVersionOfFiles", null); | ||
exports.default = SchemaBuildsAndMapsPathsTest; | ||
//# sourceMappingURL=SchemaBuildsAndMapsPaths.test.js.map |
@@ -18,29 +18,7 @@ "use strict"; | ||
} | ||
// const DIVIDER = "\n\n\n************************************************\n\n\n"; | ||
function copyAndMap(options) { | ||
assert(options.cwd, "You must pass options.cwd. This is where I'll look for the schema module (root of workspace if in monorepo)"); | ||
assert(options.destination, 'You need to pass a options.destination (sub project if mono repo)'); | ||
// places to look for schema | ||
const target = path_1.default.join(options.cwd, 'node_modules', '@sprucelabs', 'schema'); | ||
const destination = path_1.default.join(options.destination, 'node_modules', '@sprucelabs', 'schema'); | ||
const schemaNodeModules = path_1.default.join(destination, 'node_modules'); | ||
// clear out destination if it exists (and does not match the target) | ||
if (target !== destination) { | ||
if (fs_1.default.existsSync(destination)) { | ||
rimraf_1.default.sync(schemaNodeModules); | ||
} | ||
// copy schema over | ||
fs_extra_1.default.copySync(target, destination); | ||
} | ||
// clear out schemas' node_modules | ||
if (fs_1.default.existsSync(schemaNodeModules)) { | ||
rimraf_1.default.sync(schemaNodeModules); | ||
} | ||
// now map paths to the new schema using the config of the destination | ||
const config = tsconfig_paths_1.loadConfig(options.destination); | ||
if (config.resultType === 'failed') { | ||
throw new Error(config.message); | ||
} | ||
const { absoluteBaseUrl, paths } = config; | ||
const resolver = tsconfig_paths_1.createMatchPath(absoluteBaseUrl, paths); | ||
const destination = ensureDirsAndResolveDestination(options); | ||
let { outResolver, srcResolver } = buildResolvers(destination); | ||
const files = globby_1.default.sync(path_1.default.join(destination, '**/*.js')); | ||
@@ -53,4 +31,10 @@ files.forEach((file) => { | ||
const search = match.replace(/"/g, ''); | ||
const resolved = resolver(search + '.js'); | ||
let resolved; | ||
if (outResolver) { | ||
resolved = outResolver(search, undefined, undefined, ['.js']); | ||
} | ||
if (!resolved) { | ||
resolved = srcResolver(search, undefined, undefined, ['.ts', '.js']); | ||
} | ||
if (!resolved) { | ||
throw new Error(`Could not map ${search}.`); | ||
@@ -66,2 +50,40 @@ } | ||
exports.copyAndMap = copyAndMap; | ||
function buildResolvers(destination) { | ||
const config = tsconfig_paths_1.loadConfig(destination); | ||
if (config.resultType === 'failed') { | ||
throw new Error(config.message); | ||
} | ||
const { paths, absoluteBaseUrl } = config; | ||
const srcResolver = tsconfig_paths_1.createMatchPath(absoluteBaseUrl, paths); | ||
const outResolver = buildOutResolver(config); | ||
return { outResolver, srcResolver }; | ||
} | ||
function buildOutResolver(config) { | ||
const fullTsConfig = JSON.parse(fs_1.default.readFileSync(config.configFileAbsolutePath).toString()); | ||
const { compilerOptions: { outDir }, } = fullTsConfig; | ||
let outResolver; | ||
if (outDir) { | ||
const resolvedOutDir = path_1.default.join(path_1.default.dirname(config.configFileAbsolutePath), outDir); | ||
outResolver = tsconfig_paths_1.createMatchPath(resolvedOutDir, config.paths); | ||
} | ||
return outResolver; | ||
} | ||
function ensureDirsAndResolveDestination(options) { | ||
const target = path_1.default.join(options.cwd, 'node_modules', '@sprucelabs', 'schema'); | ||
const destination = path_1.default.join(options.destination, 'node_modules', '@sprucelabs', 'schema'); | ||
const schemaNodeModules = path_1.default.join(destination, 'node_modules'); | ||
// clear out destination if it exists (and does not match the target) | ||
if (target !== destination) { | ||
if (fs_1.default.existsSync(destination)) { | ||
rimraf_1.default.sync(schemaNodeModules); | ||
} | ||
// copy schema over | ||
fs_extra_1.default.copySync(target, destination); | ||
} | ||
// clear out schemas' node_modules | ||
if (fs_1.default.existsSync(schemaNodeModules)) { | ||
rimraf_1.default.sync(schemaNodeModules); | ||
} | ||
return destination; | ||
} | ||
function default_1(_, options) { | ||
@@ -68,0 +90,0 @@ copyAndMap(options); |
@@ -0,1 +1,8 @@ | ||
# [1.1.0](https://github.com/sprucelabsai/babel-plugin-schema/compare/v1.0.22...v1.1.0) (2020-08-13) | ||
### Features | ||
* checks outDir first for built file ([037e2f8](https://github.com/sprucelabsai/babel-plugin-schema/commit/037e2f8)) | ||
## [1.0.22](https://github.com/sprucelabsai/babel-plugin-schema/compare/v1.0.21...v1.0.22) (2020-08-12) | ||
@@ -2,0 +9,0 @@ |
115
package.json
{ | ||
"name": "@sprucelabs/babel-plugin-schema", | ||
"version": "1.0.22", | ||
"description": "Builds @sprucelabs/schema and generates static paths to #spruce directories.", | ||
"main": "build/index.js", | ||
"repository": "git@github.com:sprucelabsai/babel-plugin-schema.git", | ||
"author": "Spruce Labs", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "jest", | ||
"test.watch": "jest --watch", | ||
"build": "npm run clean && npm run build.tsc", | ||
"build.tsc": "tsc", | ||
"build.watch": "tsc -w", | ||
"clean": "rm -rf build/", | ||
"lint": "eslint '**/*.ts' && tsc -p . --noEmit", | ||
"lint.fix": "eslint --fix '**/*.ts'", | ||
"lint.watch": "chokidar 'src/**/*' '../spruce-templates/src/**' -c 'yarn lint.tsc'", | ||
"release": "semantic-release", | ||
"upgrade.packages": "yarn upgrade --latest && rm yarn.lock | true && yarn | true && yarn lint.fix | true", | ||
"upgrade.packages.all": "yarn upgrade.packages", | ||
"upgrade.packages.test": "yarn upgrade.packages.all && yarn lint && yarn build && yarn test" | ||
}, | ||
"dependencies": { | ||
"fs-extra": "^9.0.1", | ||
"globby": "^11.0.1", | ||
"rimraf": "^3.0.2", | ||
"tsconfig-paths": "^3.9.0" | ||
}, | ||
"devDependencies": { | ||
"@sprucelabs/semantic-release": "^4.0.5", | ||
"@sprucelabs/test": "^5.3.147", | ||
"@types/fs-extra": "^9.0.1", | ||
"@types/rimraf": "^3.0.0", | ||
"eslint": "^7.6.0", | ||
"eslint-config-spruce": "^10.8.8", | ||
"jest": "^26.4.0", | ||
"prettier": "^2.0.5", | ||
"typescript": "^3.9.7" | ||
}, | ||
"jest": { | ||
"maxWorkers": 4, | ||
"testTimeout": 120000, | ||
"testEnvironment": "node", | ||
"testPathIgnorePatterns": [ | ||
"<rootDir>/tmp/", | ||
"<rootDir>/src/", | ||
"<rootDir>/node_modules/", | ||
"<rootDir>/build/__tests__/testDirsAndFiles/" | ||
], | ||
"testMatch": [ | ||
"**/__tests__/**/*.test.js?(x)" | ||
], | ||
"moduleNameMapper": { | ||
"^#spruce/(.*)$": "<rootDir>/build/.spruce/$1" | ||
} | ||
} | ||
"name": "@sprucelabs/babel-plugin-schema", | ||
"version": "1.1.0", | ||
"description": "Builds @sprucelabs/schema and generates static paths to #spruce directories.", | ||
"main": "build/index.js", | ||
"repository": "git@github.com:sprucelabsai/babel-plugin-schema.git", | ||
"author": "Spruce Labs", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "jest", | ||
"test.watch": "jest --watch", | ||
"build": "npm run clean && npm run build.tsc && yarn copy-test-files", | ||
"build.tsc": "tsc", | ||
"build.watch": "concurrently 'tsc -w' 'chokidar \"src/__tests__/files/*\" -c yarn copy-test-files'", | ||
"clean": "rm -rf build/", | ||
"lint": "eslint '**/*.ts' && tsc -p . --noEmit", | ||
"lint.fix": "eslint --fix '**/*.ts'", | ||
"lint.watch": "chokidar 'src/**/*' '../spruce-templates/src/**' -c 'yarn lint.tsc'", | ||
"release": "semantic-release", | ||
"upgrade.packages.all": "yarn upgrade --latest && rm yarn.lock | true && yarn | true && yarn lint.fix | true", | ||
"upgrade.packages.test": "yarn upgrade.packages.all && yarn lint && yarn build && yarn test", | ||
"copy-test-files": "rm -rf ./build/__tests__/files && mkdirp ./build/__tests__/files && cp -r ./src/__tests__/files/* ./build/__tests__/files" | ||
}, | ||
"dependencies": { | ||
"fs-extra": "^9.0.1", | ||
"globby": "^11.0.1", | ||
"rimraf": "^3.0.2", | ||
"tsconfig-paths": "^3.9.0" | ||
}, | ||
"devDependencies": { | ||
"@sprucelabs/semantic-release": "^4.0.5", | ||
"@sprucelabs/test": "^5.3.130", | ||
"@types/fs-extra": "^9.0.1", | ||
"@types/rimraf": "^3.0.0", | ||
"chokidar": "^3.4.2", | ||
"concurrently": "^5.3.0", | ||
"copyfiles": "^2.3.0", | ||
"eslint": "^7.6.0", | ||
"eslint-config-spruce": "^10.8.2", | ||
"jest": "^26.3.0", | ||
"prettier": "^2.0.5", | ||
"typescript": "^3.9.7" | ||
}, | ||
"jest": { | ||
"maxWorkers": 4, | ||
"testTimeout": 120000, | ||
"testEnvironment": "node", | ||
"testPathIgnorePatterns": [ | ||
"<rootDir>/tmp/", | ||
"<rootDir>/src/", | ||
"<rootDir>/node_modules/", | ||
"<rootDir>/build/__tests__/testDirsAndFiles/" | ||
], | ||
"testMatch": [ | ||
"**/__tests__/**/*.test.js?(x)" | ||
], | ||
"moduleNameMapper": { | ||
"^#spruce/(.*)$": "<rootDir>/build/.spruce/$1" | ||
} | ||
} | ||
} |
import { exec } from 'child_process' | ||
import os from 'os' | ||
import pathUtil from 'path' | ||
import AbstractSpruceTest, { test, assert } from '@sprucelabs/test' | ||
import fsUtil from 'fs-extra' | ||
import rimraf from 'rimraf' | ||
import { copyAndMap } from '../../index' | ||
export default class SchemaBuildsAndMapsPathsTest extends AbstractSpruceTest { | ||
protected static testDirsToDelete: string[] = [] | ||
protected static async afterAll() { | ||
super.afterAll() | ||
for (const dir of this.testDirsToDelete) { | ||
rimraf.sync(dir) | ||
} | ||
} | ||
@test() | ||
@@ -13,3 +22,16 @@ protected static async buildsSchemaWithoutError() { | ||
const fieldFactoryFile = pathUtil.join( | ||
const fieldFactoryFile = this.fieldFactoryFilepath(cwd) | ||
const contents = fsUtil.readFileSync(fieldFactoryFile).toString() | ||
assert.doesInclude(contents, '#spruce') | ||
copyAndMap({ cwd, destination: cwd }) | ||
const afterMapContents = fsUtil.readFileSync(fieldFactoryFile).toString() | ||
assert.doesNotInclude(afterMapContents, '#spruce') | ||
assert.doesInclude(afterMapContents, '@sprucelabs/schema/build/.spruce/') | ||
} | ||
private static fieldFactoryFilepath(cwd: string) { | ||
return this.resolvePath( | ||
cwd, | ||
@@ -19,15 +41,75 @@ 'node_modules/@sprucelabs/schema', | ||
) | ||
const contents = fsUtil.readFileSync(fieldFactoryFile).toString() | ||
} | ||
assert.doesInclude(contents, '#spruce') | ||
@test() | ||
protected static async buildsSchemaAndUsesTheHashSpruceVersionOfFiles() { | ||
const cwd = await this.setupNewPackage() | ||
// copy schema files | ||
const sourceHashSpruce = this.resolvePath( | ||
cwd, | ||
'node_modules', | ||
'@sprucelabs/schema', | ||
'build', | ||
'.spruce' | ||
) | ||
const destinationHashSpruce = this.resolvePath(cwd, 'build', '.spruce') | ||
await this.copyDir(sourceHashSpruce, destinationHashSpruce) | ||
// copy ts config | ||
const sourceTsConfig = this.resolvePath( | ||
'src', | ||
'__tests__', | ||
'files', | ||
'test-tsconfig.json' | ||
) | ||
const tsConfigContents = fsUtil.readFileSync(sourceTsConfig) | ||
const destinationTsConfig = this.resolvePath(cwd, 'tsconfig.json') | ||
fsUtil.writeFileSync(destinationTsConfig, tsConfigContents) | ||
copyAndMap({ cwd, destination: cwd }) | ||
const fieldFactoryFile = this.fieldFactoryFilepath(cwd) | ||
const afterMapContents = fsUtil.readFileSync(fieldFactoryFile).toString() | ||
assert.doesNotInclude(afterMapContents, '#spruce') | ||
assert.doesNotInclude(afterMapContents, '@sprucelabs/schema/build/.spruce/') | ||
} | ||
private static async copyDir(source: string, destination: string) { | ||
fsUtil.ensureDir(destination) | ||
return new Promise((resolve) => { | ||
exec( | ||
`cd ${source} && tar cf - . | (cd ${destination}; tar xf -)`, | ||
{ maxBuffer: 1024 * 1024 * 5 }, | ||
(err, stdout) => { | ||
if (err) { | ||
throw err | ||
} | ||
resolve(stdout) | ||
} | ||
) | ||
}) | ||
} | ||
private static async setupNewPackage() { | ||
const cwd = await this.setupNewCwd() | ||
this.testDirsToDelete.push(cwd) | ||
await this.invokeYarnCommands(cwd) | ||
this.copyTsConfig(cwd) | ||
return cwd | ||
} | ||
private static async setupNewCwd() { | ||
const today = new Date() | ||
const cwd = pathUtil.join( | ||
const cwd = this.resolvePath( | ||
os.tmpdir(), | ||
@@ -40,2 +122,17 @@ 'babel-plugin-schema', | ||
return cwd | ||
} | ||
private static copyTsConfig(cwd: string) { | ||
const tsConfigContents = fsUtil.readFileSync( | ||
this.resolvePath(__dirname, '..', '..', '..', 'tsconfig.json') | ||
) | ||
fsUtil.writeFileSync( | ||
this.resolvePath(cwd, 'tsconfig.json'), | ||
tsConfigContents | ||
) | ||
} | ||
private static async invokeYarnCommands(cwd: string) { | ||
await new Promise((resolve, reject) => { | ||
@@ -59,9 +156,3 @@ exec( | ||
}) | ||
const tsConfigContents = fsUtil.readFileSync( | ||
pathUtil.join(__dirname, '..', '..', '..', 'tsconfig.json') | ||
) | ||
fsUtil.writeFileSync(pathUtil.join(cwd, 'tsconfig.json'), tsConfigContents) | ||
return cwd | ||
} | ||
} |
120
src/index.ts
@@ -6,3 +6,8 @@ import fs from 'fs' | ||
import rimRaf from 'rimraf' | ||
import { loadConfig, createMatchPath } from 'tsconfig-paths' | ||
import { | ||
loadConfig, | ||
createMatchPath, | ||
MatchPath, | ||
ConfigLoaderSuccessResult, | ||
} from 'tsconfig-paths' | ||
@@ -20,3 +25,2 @@ function assert(truthy: any, message: string) { | ||
// const DIVIDER = "\n\n\n************************************************\n\n\n"; | ||
export function copyAndMap(options: PluginOptions) { | ||
@@ -32,3 +36,83 @@ assert( | ||
// places to look for schema | ||
const destination = ensureDirsAndResolveDestination(options) | ||
let { outResolver, srcResolver } = buildResolvers(destination) | ||
const files = globby.sync(pathUtil.join(destination, '**/*.js')) | ||
files.forEach((file) => { | ||
let contents = fs.readFileSync(file).toString() | ||
let found = false | ||
contents = `${contents}`.replace(/"#spruce\/(.*?)"/gi, (match) => { | ||
found = true | ||
const search = match.replace(/"/g, '') | ||
let resolved: string | undefined | ||
if (outResolver) { | ||
resolved = outResolver(search, undefined, undefined, ['.js']) | ||
} | ||
if (!resolved) { | ||
resolved = srcResolver(search, undefined, undefined, ['.ts', '.js']) | ||
} | ||
if (!resolved) { | ||
throw new Error(`Could not map ${search}.`) | ||
} | ||
return `"${resolved}"` | ||
}) | ||
if (found) { | ||
fs.writeFileSync(file, contents) | ||
} | ||
}) | ||
} | ||
function buildResolvers( | ||
destination: string | ||
): { | ||
outResolver: MatchPath | undefined | ||
srcResolver: MatchPath | ||
} { | ||
const config = loadConfig(destination) | ||
if (config.resultType === 'failed') { | ||
throw new Error(config.message) | ||
} | ||
const { paths, absoluteBaseUrl } = config | ||
const srcResolver = createMatchPath(absoluteBaseUrl, paths) | ||
const outResolver = buildOutResolver(config) | ||
return { outResolver, srcResolver } | ||
} | ||
function buildOutResolver( | ||
config: ConfigLoaderSuccessResult | ||
): MatchPath | undefined { | ||
const fullTsConfig = JSON.parse( | ||
fs.readFileSync(config.configFileAbsolutePath).toString() | ||
) | ||
const { | ||
compilerOptions: { outDir }, | ||
} = fullTsConfig | ||
let outResolver: MatchPath | undefined | ||
if (outDir) { | ||
const resolvedOutDir = pathUtil.join( | ||
pathUtil.dirname(config.configFileAbsolutePath), | ||
outDir | ||
) | ||
outResolver = createMatchPath(resolvedOutDir, config.paths) | ||
} | ||
return outResolver | ||
} | ||
function ensureDirsAndResolveDestination(options: PluginOptions) { | ||
const target = pathUtil.join( | ||
@@ -64,31 +148,3 @@ options.cwd, | ||
} | ||
// now map paths to the new schema using the config of the destination | ||
const config = loadConfig(options.destination) | ||
if (config.resultType === 'failed') { | ||
throw new Error(config.message) | ||
} | ||
const { absoluteBaseUrl, paths } = config | ||
const resolver = createMatchPath(absoluteBaseUrl, paths) | ||
const files = globby.sync(pathUtil.join(destination, '**/*.js')) | ||
files.forEach((file) => { | ||
let contents = fs.readFileSync(file).toString() | ||
let found = false | ||
contents = `${contents}`.replace(/"#spruce\/(.*?)"/gi, (match) => { | ||
found = true | ||
const search = match.replace(/"/g, '') | ||
const resolved = resolver(search + '.js') | ||
if (!resolved) { | ||
throw new Error(`Could not map ${search}.`) | ||
} | ||
return `"${resolved}"` | ||
}) | ||
if (found) { | ||
fs.writeFileSync(file, contents) | ||
} | ||
}) | ||
return destination | ||
} | ||
@@ -95,0 +151,0 @@ |
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"esModuleInterop": true, | ||
"target": "ES2017", | ||
"lib": [ | ||
"es2017", | ||
"ES2020" | ||
], | ||
"declaration": true, | ||
"noImplicitAny": true, | ||
"allowJs": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noImplicitReturns": true, | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"resolveJsonModule": true, | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"outDir": "build", | ||
"baseUrl": "src", | ||
"experimentalDecorators": true, | ||
"paths": { | ||
"#spruce/*": [ | ||
"../node_modules/@sprucelabs/schema/build/.spruce/*", | ||
] | ||
} | ||
}, | ||
"include": [ | ||
"./*.ts", | ||
"./**/*.ts" | ||
], | ||
"exclude": [ | ||
"build" | ||
] | ||
} | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"esModuleInterop": true, | ||
"target": "ES2017", | ||
"lib": ["es2017", "ES2020"], | ||
"declaration": true, | ||
"noImplicitAny": true, | ||
"allowJs": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noImplicitReturns": true, | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"resolveJsonModule": true, | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"outDir": "build", | ||
"baseUrl": "src", | ||
"experimentalDecorators": true, | ||
"paths": { | ||
"#spruce/*": ["../node_modules/@sprucelabs/schema/build/.spruce/*"] | ||
} | ||
}, | ||
"include": ["./*.ts", "./**/*.ts"], | ||
"exclude": ["build"] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38567
21
663
12