-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re: #26
- Loading branch information
Showing
10 changed files
with
206 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Remove the .tshy-build-tmp folder, but ONLY if | ||
// the "incremental" config value is not set, or if | ||
// it does not contain any tsbuildinfo files. | ||
// If we are in incremental mode, and have tsbuildinfo files, | ||
// then find and remove any files here that do not have a matching | ||
// source file in ./src | ||
|
||
import { readdirSync } from 'fs' | ||
import { parse } from 'path' | ||
import { rimrafSync } from 'rimraf' | ||
import * as console from './console.js' | ||
import readTypescriptConfig from './read-typescript-config.js' | ||
|
||
const cleanRemovedOutputs = (path: string, root: string) => { | ||
const entries = readdirSync(`${root}/${path}`, { | ||
withFileTypes: true, | ||
}) | ||
let sources: Set<string> | undefined = undefined | ||
try { | ||
sources = new Set(readdirSync(`src/${path}`)) | ||
} catch {} | ||
// directory was removed | ||
if (!sources) { | ||
return rimrafSync(`${root}/${path}`) | ||
} | ||
for (const e of entries) { | ||
const outputFile = `${path}/${e.name}` | ||
if (e.isDirectory()) { | ||
cleanRemovedOutputs(outputFile, root) | ||
continue | ||
} | ||
let { ext, name } = parse(outputFile) | ||
if (ext === '.map') { | ||
continue | ||
} | ||
if (name.endsWith('.d') && ext.endsWith('ts')) { | ||
ext = '.d' + ext | ||
name = name.substring(0, name.length - '.d'.length) | ||
} | ||
|
||
const inputSearch = | ||
ext === '.js' || ext === '.d.ts' | ||
? ['.tsx', '.ts'] | ||
: ext === '.mjs' || ext === '.d.mts' | ||
? ['.mts'] | ||
: ext === '.cjs' || ext === '.d.cts' | ||
? ['.cts'] | ||
: [] | ||
inputSearch.push(ext) | ||
let del = true | ||
for (const ext of inputSearch) { | ||
if (sources.has(`${name}${ext}`)) { | ||
del = false | ||
break | ||
} | ||
} | ||
if (del) { | ||
console.debug('removing output file', outputFile) | ||
rimrafSync([ | ||
`${root}/${outputFile}`, | ||
`${root}/${outputFile}.map`, | ||
]) | ||
} | ||
} | ||
} | ||
|
||
export default () => { | ||
const config = readTypescriptConfig() | ||
if (config.options.incremental !== true) { | ||
return rimrafSync('.tshy-build-tmp') | ||
} | ||
|
||
let buildInfos: string[] | undefined = undefined | ||
try { | ||
buildInfos = readdirSync('.tshy-build-tmp/.tshy') | ||
} catch {} | ||
if (!buildInfos?.length) { | ||
return rimrafSync('.tshy-build-tmp') | ||
} | ||
|
||
// delete anything that has been removed from src. | ||
for (const dialect of readdirSync('.tshy-build-tmp')) { | ||
if (dialect === '.tshy') continue | ||
cleanRemovedOutputs('.', `.tshy-build-tmp/${dialect}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// read the actual configuration that tsc is using | ||
// Note: cannot just use JSON.parse, because ts config files | ||
// are jsonc. | ||
import { resolve } from 'path' | ||
import ts from 'typescript' | ||
const { readFile } = ts.sys | ||
|
||
let parsedTsConfig: ts.ParsedCommandLine | undefined = undefined | ||
export default () => { | ||
if (parsedTsConfig) return parsedTsConfig | ||
const configPath = resolve('tsconfig.json') | ||
const readResult = ts.readConfigFile(configPath, readFile) | ||
return (parsedTsConfig = ts.parseJsonConfigFileContent( | ||
readResult.config, | ||
ts.sys, | ||
process.cwd() | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { statSync } from 'fs' | ||
import t from 'tap' | ||
import cleanBuildTmp from '../src/clean-build-tmp.js' | ||
import readTypescriptConfig from '../src/read-typescript-config.js' | ||
const cwd = process.cwd() | ||
t.afterEach(() => process.chdir(cwd)) | ||
|
||
t.test('no incremental build, just delete it', t => { | ||
readTypescriptConfig().options.incremental = false | ||
process.chdir( | ||
t.testdir({ | ||
'.tshy-build-tmp': {}, | ||
}) | ||
) | ||
cleanBuildTmp() | ||
t.throws(() => statSync('.tshy-build-tmp')) | ||
t.end() | ||
}) | ||
|
||
t.test('no tsbuildinfo, just delete it', t => { | ||
readTypescriptConfig().options.incremental = true | ||
process.chdir( | ||
t.testdir({ | ||
'.tshy-build-tmp': {}, | ||
}) | ||
) | ||
cleanBuildTmp() | ||
t.throws(() => statSync('.tshy-build-tmp')) | ||
t.end() | ||
}) | ||
|
||
t.test('remove files not found in src', t => { | ||
readTypescriptConfig().options.incremental = true | ||
process.chdir( | ||
t.testdir({ | ||
'.tshy-build-tmp': { | ||
'.tshy': { 'esm.tsbuildinfo': '{}' }, | ||
esm: { | ||
'a.d.ts': '', | ||
'a.js': '', | ||
'a.js.map': '', | ||
'a.d.ts.map': '', | ||
'a.jsx': '', | ||
'a.jsx.map': '', | ||
dir: { | ||
'b.d.ts': '', | ||
'b.js': '', | ||
'b.js.map': '', | ||
'b.d.ts.map': '', | ||
}, | ||
'x.d.ts': '', | ||
'x.js': '', | ||
'x.js.map': '', | ||
'x.d.ts.map': '', | ||
'm.mjs': '', | ||
'm.d.mts': '', | ||
'c.cjs': '', | ||
'c.d.cts': '', | ||
xdir: { | ||
'x.d.ts': '', | ||
'x.js': '', | ||
'x.js.map': '', | ||
'x.d.ts.map': '', | ||
}, | ||
}, | ||
}, | ||
src: { | ||
'a.tsx': '', | ||
dir: { | ||
'b.ts': '', | ||
}, | ||
}, | ||
}) | ||
) | ||
cleanBuildTmp() | ||
t.throws(() => statSync('.tshy-build-tmp/dist/esm/x.js.map')) | ||
t.throws(() => statSync('.tshy-build-tmp/dist/esm/x.d.ts')) | ||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import t from 'tap' | ||
import readTypescriptConfig from '../src/read-typescript-config.js' | ||
|
||
const config = readTypescriptConfig() | ||
t.equal(config, readTypescriptConfig(), 'cached') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters