Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch JS/TS API wrappers to ES modules #456

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 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 All @@ -310,11 +336,6 @@ task verifyTsDeclarations(type: NodeTask, dependsOn: compileTs) {
execOverrides {
it.workingDir = typescriptFolder
}
args = [
'--require', 'ts-node/register',
'--require', 'jsdom-global/register',
'./tests/**/*.ts'
]
}

// Add `carp.test` helpers.
Expand Down
4 changes: 4 additions & 0 deletions typescript-declarations/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"spec": ["tests"],
"color": true
}
26 changes: 13 additions & 13 deletions typescript-declarations/carp-common/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as extend from "@cachet/carp-common-generated"
import * as kotlinStdLib from "@cachet/carp-kotlin"
import * as kotlinDateTime from "@cachet/carp-kotlinx-datetime"
import * as kotlinSerialization from "@cachet/carp-kotlinx-serialization"
import extend from "@cachet/carp-common-generated"
import kotlinStdLib from "@cachet/carp-kotlin"
import kotlinDateTime from "@cachet/carp-kotlinx-datetime"
import kotlinSerialization from "@cachet/carp-kotlinx-serialization"


declare module "@cachet/carp-common-generated"
{
// Declare missing types for which no imports were generated.
namespace kotlin
{
type Long = kotlinStdLib.kotlin.Long
type Long = kotlinStdLib.Long
}
namespace kotlin.reflect
{
Expand All @@ -19,24 +19,24 @@ declare module "@cachet/carp-common-generated"
}
namespace kotlin.time
{
type Duration = kotlinStdLib.kotlin.time.Duration
type Duration = kotlinStdLib.time.Duration
}
namespace kotlin.collections
{
type List<T> = kotlinStdLib.kotlin.collections.List<T>
type Set<T> = kotlinStdLib.kotlin.collections.Set<T>
type Map<K, V> = kotlinStdLib.kotlin.collections.Map<K, V>
type List<T> = kotlinStdLib.collections.List<T>
type Set<T> = kotlinStdLib.collections.Set<T>
type Map<K, V> = kotlinStdLib.collections.Map<K, V>
}
namespace kotlinx.datetime
{
type Instant = kotlinDateTime.kotlinx.datetime.Instant
type Instant = kotlinDateTime.datetime.Instant
}
namespace kotlinx.serialization.json
{
type Json = kotlinSerialization.kotlinx.serialization.json.Json
type Json = kotlinSerialization.serialization.json.Json
}
}


// Export facade.
export * from "@cachet/carp-common-generated"
// Re-export augmented types.
export { default } from "@cachet/carp-common-generated"
2 changes: 2 additions & 0 deletions typescript-declarations/carp-common/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "@cachet/carp-common",
"type": "module",
"main": "index.js",
"version": "1.1.2"
}
2 changes: 1 addition & 1 deletion typescript-declarations/carp-data-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as extend from "@cachet/carp-data-core-generated"
import * as kotlinStdLib from "@cachet/carp-kotlin"
import * as kotlinDateTime from "@cachet/carp-kotlinx-datetime"
import * as kotlinSerialization from "@cachet/carp-kotlinx-serialization"
import * as carpCommon from "@cachet/carp-common"
import carpCommon from "@cachet/carp-common"


declare module "@cachet/carp-data-core-generated"
Expand Down
2 changes: 2 additions & 0 deletions typescript-declarations/carp-data-core/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "@cachet/carp-data-core",
"type": "module",
"main": "index.js",
"version": "1.1.2"
}
28 changes: 14 additions & 14 deletions typescript-declarations/carp-deployments-core/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as extend from "@cachet/carp-deployments-core-generated"
import * as kotlinStdLib from "@cachet/carp-kotlin"
import * as kotlinDateTime from "@cachet/carp-kotlinx-datetime"
import * as kotlinSerialization from "@cachet/carp-kotlinx-serialization"
import * as carpCommon from "@cachet/carp-common"
import extend from "@cachet/carp-deployments-core-generated"
import kotlinStdLib from "@cachet/carp-kotlin"
import kotlinDateTime from "@cachet/carp-kotlinx-datetime"
import kotlinSerialization from "@cachet/carp-kotlinx-serialization"
import carpCommon from "@cachet/carp-common"


declare module "@cachet/carp-deployments-core-generated"
{
// Declare missing types for which no imports were generated.
namespace kotlin
{
type Long = kotlinStdLib.kotlin.Long
type Long = kotlinStdLib.Long
}
namespace kotlin.reflect
{
Expand All @@ -20,21 +20,21 @@ declare module "@cachet/carp-deployments-core-generated"
}
namespace kotlin.time
{
type Duration = kotlinStdLib.kotlin.time.Duration
type Duration = kotlinStdLib.time.Duration
}
namespace kotlin.collections
{
type List<T> = kotlinStdLib.kotlin.collections.List<T>
type Set<T> = kotlinStdLib.kotlin.collections.Set<T>
type Map<K, V> = kotlinStdLib.kotlin.collections.Map<K, V>
type List<T> = kotlinStdLib.collections.List<T>
type Set<T> = kotlinStdLib.collections.Set<T>
type Map<K, V> = kotlinStdLib.collections.Map<K, V>
}
namespace kotlinx.datetime
{
type Instant = kotlinDateTime.kotlinx.datetime.Instant
type Instant = kotlinDateTime.datetime.Instant
}
namespace kotlinx.serialization.json
{
type Json = kotlinSerialization.kotlinx.serialization.json.Json
type Json = kotlinSerialization.serialization.json.Json
}
}

Expand All @@ -43,5 +43,5 @@ declare module "@cachet/carp-deployments-core-generated"
extend.dk.cachet.carp.common = carpCommon.dk.cachet.carp.common as any;


// Export facade.
export * from "@cachet/carp-deployments-core-generated"
// Re-export augmented types.
export { default } from "@cachet/carp-deployments-core-generated"
2 changes: 2 additions & 0 deletions typescript-declarations/carp-deployments-core/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "@cachet/carp-deployments-core",
"type": "module",
"main": "index.js",
"version": "1.1.2"
}
55 changes: 28 additions & 27 deletions typescript-declarations/carp-kotlin/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as kotlinStdLib from "@cachet/kotlin-kotlin-stdlib"
/// <reference path="kotlin-kotlin-stdlib.d.ts" />
import extend from "@cachet/kotlin-kotlin-stdlib"


// Facade with better method names and type conversions for internal types.
Expand All @@ -9,11 +10,11 @@ export namespace kotlin
{
toNumber(): number
}
export const toLong: (number: number) => Long = kotlinStdLib.$_$.toLong_0
export const toLong: (number: number) => Long = extend.$_$.toLong_0
export class Pair<K, V>
{
constructor( first: K, second: V ) {
let kotlinPair = new kotlinStdLib.$_$.Pair( first, second );
let kotlinPair = new extend.$_$.Pair( first, second );
kotlinPair.first = kotlinPair.md_1;
kotlinPair.second = kotlinPair.nd_1;
return kotlinPair;
Expand All @@ -38,12 +39,12 @@ export namespace kotlin.collections
keys: Set<K>
values: Collection<V>
}
export const listOf: <T>(array: T[]) => List<T> = kotlinStdLib.$_$.listOf_0
export const setOf: <T>(array: T[]) => Set<T> = kotlinStdLib.$_$.setOf_0
export const listOf: <T>(array: T[]) => List<T> = extend.$_$.listOf_0
export const setOf: <T>(array: T[]) => Set<T> = extend.$_$.setOf_0
export const mapOf =
function<K, V>( pairs: kotlin.Pair<K, V>[] ): Map<K, V>
{
return kotlinStdLib.$_$.mapOf_0( pairs as any )
return extend.$_$.mapOf_0( pairs as any )
}
}
export namespace kotlin.time
Expand All @@ -55,7 +56,7 @@ export namespace kotlin.time
}
export namespace Duration
{
export const Companion: any = kotlinStdLib.$_$.Companion_getInstance_13()
export const Companion: any = extend.$_$.Companion_getInstance_13()
export const parseIsoString: (isoDuration: string) => Duration = Companion.zf
export const ZERO: Duration = Companion.wf_1
export const INFINITE: Duration = Companion.xf_1
Expand Down Expand Up @@ -97,37 +98,37 @@ declare module "@cachet/kotlin-kotlin-stdlib"


// Implement base interfaces in internal types.
kotlinStdLib.$_$.Long.prototype.toNumber = function(): number { return this.da(); };
Object.defineProperty( kotlinStdLib.$_$.Long.prototype, "inWholeMilliseconds", {
extend.$_$.Long.prototype.toNumber = function(): number { return this.da(); };
Object.defineProperty( extend.$_$.Long.prototype, "inWholeMilliseconds", {
get: function inWholeMilliseconds()
{
return kotlinStdLib.$_$._Duration___get_inWholeMilliseconds__impl__msfiry(this).toNumber();
return extend.$_$._Duration___get_inWholeMilliseconds__impl__msfiry(this).toNumber();
}
} );
Object.defineProperty( kotlinStdLib.$_$.Long.prototype, "inWholeMicroseconds", {
Object.defineProperty( extend.$_$.Long.prototype, "inWholeMicroseconds", {
get: function inWholeMicroseconds()
{
return kotlinStdLib.$_$._Duration___get_inWholeMicroseconds__impl__8oe8vv(this).toNumber();
return extend.$_$._Duration___get_inWholeMicroseconds__impl__8oe8vv(this).toNumber();
}
} );
kotlinStdLib.$_$.EmptyList.prototype.contains = function<T>( value: T ): boolean { return false; }
kotlinStdLib.$_$.EmptyList.prototype.size = function<T>(): number { return 0; }
kotlinStdLib.$_$.EmptyList.prototype.toArray = function<T>(): T[] { return []; }
kotlinStdLib.$_$.AbstractMutableList.prototype.contains = function<T>( value: T ): boolean { return this.p( value ); }
kotlinStdLib.$_$.AbstractMutableList.prototype.size = function<T>(): number { return this.n(); }
kotlinStdLib.$_$.EmptySet.prototype.contains = function<T>( value: T ): boolean { return false; }
kotlinStdLib.$_$.EmptySet.prototype.size = function<T>(): number { return 0; }
kotlinStdLib.$_$.EmptySet.prototype.toArray = function<T>(): T[] { return []; }
kotlinStdLib.$_$.HashSet.prototype.contains = function<T>( value: T ): boolean { return this.p( value ); }
kotlinStdLib.$_$.HashSet.prototype.size = function<T>(): number { return this.n(); }
kotlinStdLib.$_$.HashMap.prototype.get = function<K, V>( key: K ): V { return this.x2( key ); }
Object.defineProperty( kotlinStdLib.$_$.HashMap.prototype, "keys", {
extend.$_$.EmptyList.prototype.contains = function<T>( value: T ): boolean { return false; }
extend.$_$.EmptyList.prototype.size = function<T>(): number { return 0; }
extend.$_$.EmptyList.prototype.toArray = function<T>(): T[] { return []; }
extend.$_$.AbstractMutableList.prototype.contains = function<T>( value: T ): boolean { return this.p( value ); }
extend.$_$.AbstractMutableList.prototype.size = function<T>(): number { return this.n(); }
extend.$_$.EmptySet.prototype.contains = function<T>( value: T ): boolean { return false; }
extend.$_$.EmptySet.prototype.size = function<T>(): number { return 0; }
extend.$_$.EmptySet.prototype.toArray = function<T>(): T[] { return []; }
extend.$_$.HashSet.prototype.contains = function<T>( value: T ): boolean { return this.p( value ); }
extend.$_$.HashSet.prototype.size = function<T>(): number { return this.n(); }
extend.$_$.HashMap.prototype.get = function<K, V>( key: K ): V { return this.x2( key ); }
Object.defineProperty( extend.$_$.HashMap.prototype, "keys", {
get: function keys() { return this.l2(); }
} );
Object.defineProperty( kotlinStdLib.$_$.HashMap.prototype, "values", {
Object.defineProperty( extend.$_$.HashMap.prototype, "values", {
get: function values() { return this.m2(); }
} );


// Re-export augmented types.
export * from "@cachet/kotlin-kotlin-stdlib";
// Export facade.
export default kotlin
2 changes: 2 additions & 0 deletions typescript-declarations/carp-kotlin/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "@cachet/carp-kotlin",
"type": "module",
"main": "index.js",
"version": "1.1.2"
}
7 changes: 4 additions & 3 deletions typescript-declarations/carp-kotlinx-datetime/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as extend from "@cachet/Kotlin-DateTime-library-kotlinx-datetime-js-ir"
/// <reference path="Kotlin-DateTime-library-kotlinx-datetime-js-ir.d.ts" />
import extend from "@cachet/Kotlin-DateTime-library-kotlinx-datetime-js-ir"


// Facade with better method names and type conversions for internal types.
Expand Down Expand Up @@ -37,5 +38,5 @@ extend.$_$.System.prototype.now = function(): kotlinx.datetime.Instant { return
extend.$_$.Instant_0.prototype.toEpochMilliseconds = function(): number { return this.y1e(); };


// Re-export augmented types.
export * from "@cachet/Kotlin-DateTime-library-kotlinx-datetime-js-ir"
// Export facade.
export default kotlinx
2 changes: 2 additions & 0 deletions typescript-declarations/carp-kotlinx-datetime/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "@cachet/carp-kotlinx-datetime",
"type": "module",
"main": "index.js",
"version": "1.1.2"
}
10 changes: 6 additions & 4 deletions typescript-declarations/carp-kotlinx-serialization/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as extendCore from "@cachet/kotlinx-serialization-kotlinx-serialization-core"
import * as extendJson from "@cachet/kotlinx-serialization-kotlinx-serialization-json"
/// <reference path="kotlinx-serialization-kotlinx-serialization-core.d.ts" />
/// <reference path="kotlinx-serialization-kotlinx-serialization-json.d.ts" />
import extendCore from "@cachet/kotlinx-serialization-kotlinx-serialization-core"
import extendJson from "@cachet/kotlinx-serialization-kotlinx-serialization-json"


// Facade with better method names and type conversions for internal types.
Expand Down Expand Up @@ -51,5 +53,5 @@ extendJson.$_$.JsonImpl.prototype.decodeFromString =
};


// Re-export augmented types.
export * from "@cachet/kotlinx-serialization-kotlinx-serialization-json"
// Export facade.
export default kotlinx
Loading
Loading