Skip to content

Commit

Permalink
Add package.json to JS test output
Browse files Browse the repository at this point in the history
Later more strict JS module systems require package.json to be present.
  • Loading branch information
Whathecode committed Feb 17, 2024
1 parent 01d4214 commit 7169590
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ buildscript {

commonModule = subprojects.find { it.name == 'carp.common' }
coreModules = subprojects.findAll { it.name.endsWith( '.core' ) }
testModules = subprojects.findAll { it.name == 'carp.common.test' || it.name == 'carp.test' }
publishNpmModule = subprojects.find { it.name == 'publish-npm-packages' }
allModules = coreModules + testModules + commonModule + publishNpmModule
devOpsModules =
subprojects.findAll {it.name == 'carp.detekt' || it.name == 'rpc' } + publishNpmModule
}
Expand Down Expand Up @@ -234,15 +236,15 @@ task setSnapshotVersion {

// TypeScript ambient declaration verification.
def typescriptFolder = 'typescript-declarations'
def npmScope = "@cachet"
apply plugin: 'com.github.node-gradle.node'
task setupTsProject(type: NpmTask) {
workingDir = file(typescriptFolder)
args = ['install']
}
task copyTestJsSources(type: Copy, dependsOn: setupTsProject) {
// Compile production sources for CARP, and the JS publication project (`publishNpmModule`).
def projects = coreModules + commonModule + publishNpmModule
projects.each {
allModules.each {
def project = it.name
dependsOn("$project:jsProductionExecutableCompileSync")
}
Expand All @@ -258,7 +260,7 @@ task copyTestJsSources(type: Copy, dependsOn: setupTsProject) {
}
eachFile { file ->
// Compiled sources have the name of the module they represent, followed by ".js" and ".d.ts".
// To be recognized by node, place them as "index.js" and "index.d.ts" in "node_modules/@cachet/<module-name>".
// To be recognized by node, place them as "index.js" and "index.d.ts" in "node_modules/<scope>/<module-name>".
def fileMatch = file.name =~ /(.+)\.(js|d\.ts)/
def moduleName = fileMatch[0][1]
def extension = fileMatch[0][2]
Expand All @@ -273,8 +275,8 @@ task copyTestJsSources(type: Copy, dependsOn: setupTsProject) {
// Modify sources to act like modules with exported named members.
file.filter { line ->
// Compiled sources refer to other modules as adjacent .js source files.
// Change these to the named modules created in the previous step.
def namedModules = line.replaceAll(~/'\.\/(.+?)\.js'/, "'@cachet/\$1'")
// Change these to the scoped modules created in the previous step.
def namedModules = line.replaceAll(~/'\.\/(.+?)\.js'/, "'$npmScope/\$1'")

// Replace `any` types with actual types for which facades are specified.
def replacedTypes = knownFacadeTypes.inject(namedModules) { curLine, type ->
Expand All @@ -299,9 +301,33 @@ task copyTestJsSources(type: Copy, dependsOn: setupTsProject) {
additionalExports
}
}
into "./$typescriptFolder/node_modules/@cachet/"
into "./$typescriptFolder/node_modules/$npmScope/"
}
task compileTs(type: NpmTask, dependsOn: copyTestJsSources) {
task packageTestJsSources(type: Copy, dependsOn: copyTestJsSources) {
allModules.each {
def project = it.name
dependsOn("$project:jsPackageJson")
dependsOn("$project:jsTestPackageJson")
}

from("$rootDir/build/js/packages") {
include "**/package.json"
includeEmptyDirs = false
}
eachFile { file ->
def moduleName = file.getFile().getParentFile().name
file.filter { line ->
// Add scope to module name.
def changedName = line.replaceAll(~/("name": ).*/, "\$1 \"$npmScope/$moduleName\",")

// Point main source to 'index.js'.
changedName.replaceAll(~/("main": ).*/, "\$1 \"index.js\",")
}

}
into "./$typescriptFolder/node_modules/$npmScope/"
}
task compileTs(type: NpmTask, dependsOn: packageTestJsSources) {
workingDir = file(typescriptFolder)
args = ['run', 'tsc']
}
Expand Down

0 comments on commit 7169590

Please sign in to comment.