Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit c229fb0

Browse files
committed
refactor(integration): port code-generator to ts
1 parent c4f3a0a commit c229fb0

File tree

10 files changed

+173
-78
lines changed

10 files changed

+173
-78
lines changed

packages/core/tsconfig.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
{
2+
"extends": "../../tsconfig.json",
23
"compilerOptions": {
3-
"strict": true,
4-
"lib": ["es5", "es2015.promise", "dom"],
5-
"target": "es5",
6-
7-
"sourceMap": true,
8-
"declaration": true,
94
"outDir": "build",
10-
"moduleResolution": "node"
5+
"lib": ["es5", "es2015.promise", "dom"]
116
},
127
"include": ["src/**/*.ts"]
138
}

packages/integrations/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
"private": true,
55
"scripts": {
66
"clean": "rimraf build",
7-
"generate:integrations": "node src/gen-integrations",
8-
"generate:readme": "node src/gen-readme",
7+
"ts": "ts-node -P tsconfig.json --files --pretty",
8+
"generate:integrations": "yarn ts src/gen-integrations",
9+
"generate:readme": "yarn ts src/gen-readme",
910
"generate": "run-p generate:*",
1011
"build": "run-s clean generate"
1112
},
@@ -14,5 +15,10 @@
1415
"markdown-table": "^1.1.2",
1516
"mustache": "^3.0.0",
1617
"yaml": "^1.0.0-rc.7"
18+
},
19+
"devDependencies": {
20+
"@types/mustache": "^0.8.31",
21+
"@types/yaml": "^1.0.0",
22+
"ts-node": "^7.0.1"
1723
}
1824
}

packages/integrations/src/gen-integrations.js renamed to packages/integrations/src/gen-integrations.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
1-
const fs = require('fs-extra')
2-
const path = require('path')
3-
const mustache = require('mustache')
4-
const pkg = require('../package.json')
1+
import fs from 'fs-extra'
2+
import path from 'path'
3+
import mustache from 'mustache'
4+
import pkg from '../package.json'
5+
6+
import integrations, { Integration } from './integration-list'
7+
8+
interface Context extends Integration {
9+
nativeModule: string
10+
out: string
11+
template: (template: string, view: {}, dest?: string) => Promise<void>
12+
}
513

614
const root = path.resolve(__dirname, '..')
7-
const integrations = require('./integration-list')
815

9-
const safeWrite = async (file, contents) => {
16+
const safeWrite = async (file: string, contents: string) => {
1017
await fs.mkdirp(path.dirname(file))
1118
await fs.writeFile(file, contents)
1219
}
1320

14-
function prepareAndroid({ template, name, android, nativeModule, slug }) {
21+
async function prepareAndroid({ template, name, android, nativeModule, slug }: Context) {
1522
const identifier = slug('.').toLowerCase()
23+
const classSlug = `${slug()}Integration`
1624
const {
1725
maven: {
18-
repo,
26+
repo = undefined,
1927
name: depName = `com.segment.analytics.android.integrations:${slug(
2028
'-'
2129
).toLowerCase()}`,
2230
version = '+@aar'
2331
} = {},
2432
factory: {
25-
class: factoryClass = `${slug()}Integration`,
26-
import: factoryImport = `com.segment.analytics.android.integrations.${identifier}.${factoryClass}`
33+
class: factoryClass = classSlug,
34+
import: factoryImport = `com.segment.analytics.android.integrations.${identifier}.${classSlug}`
2735
} = {}
2836
} = android
2937
const classpath = `com.segment.analytics.reactnative.integration.${identifier}`
3038
const dependency = `${depName}:${version}`
3139
const root = 'android/src/main'
3240

33-
return Promise.all([
41+
await Promise.all([
3442
template('android/build.gradle', { dependency, maven: repo }),
3543

3644
template(`${root}/AndroidManifest.xml`, { classpath }),
@@ -45,22 +53,25 @@ function prepareAndroid({ template, name, android, nativeModule, slug }) {
4553
])
4654
}
4755

48-
function prepareiOS({ template, name, out, ios, nativeModule, slug }) {
56+
async function prepareiOS({ template, name, out, ios, nativeModule, slug }: Context) {
4957
const xcodeProject = 'ios/RNAnalyticsIntegration.xcodeproj'
5058
const targetXcodeProject = `ios/${nativeModule}.xcodeproj`
5159
const pod_name = `RNAnalyticsIntegration-${slug('-')}`
5260
const {
5361
pod: {
5462
name: pod_dependency = `Segment-${slug()}`,
55-
version: pod_version
63+
version: pod_version = undefined
5664
} = {},
57-
prefix = 'SEG',
58-
className = `${prefix}${slug()}IntegrationFactory`,
65+
prefix = 'SEG'
66+
} = ios
67+
const classSlug = `${prefix}${slug()}IntegrationFactory`
68+
const {
69+
className = classSlug,
5970
framework = pod_dependency,
60-
header = className
71+
header = classSlug
6172
} = ios
6273

63-
return Promise.all([
74+
await Promise.all([
6475
fs.copy(
6576
path.resolve(root, 'template', xcodeProject, 'project.xcworkspace'),
6677
path.resolve(out, targetXcodeProject, 'project.xcworkspace')
@@ -90,7 +101,7 @@ function prepareiOS({ template, name, out, ios, nativeModule, slug }) {
90101
])
91102
}
92103

93-
function prepareJs({
104+
async function prepareJs({
94105
name,
95106
npm,
96107
nativeModule,
@@ -99,8 +110,8 @@ function prepareJs({
99110
ios,
100111
android,
101112
slug
102-
}) {
103-
return Promise.all([
113+
}: Context) {
114+
await Promise.all([
104115
safeWrite(
105116
path.resolve(out, 'package.json'),
106117
JSON.stringify(
@@ -124,26 +135,25 @@ function prepareJs({
124135
])
125136
}
126137

127-
function genIntegration({ name, ios, android, npm, slug }) {
138+
function genIntegration({ name, ios, android, npm, slug }: Integration) {
128139
const out = path.resolve(root, 'build', npm.package)
129140
const nativeModule = `RNAnalyticsIntegration_${slug('_')}`
130-
const template = async (template, view, dest = template) =>
131-
safeWrite(
132-
path.resolve(out, dest),
133-
mustache.render(
134-
await fs.readFile(path.resolve(root, 'template', template), 'utf-8'),
135-
view
136-
)
137-
)
138-
const ctx = {
141+
const ctx: Context = {
139142
name,
140143
out,
141144
nativeModule,
142145
npm,
143-
template,
144146
ios,
145147
android,
146-
slug
148+
slug,
149+
template: async (template, view, dest = template) =>
150+
await safeWrite(
151+
path.resolve(out, dest),
152+
mustache.render(
153+
await fs.readFile(path.resolve(root, 'template', template), 'utf-8'),
154+
view
155+
)
156+
)
147157
}
148158

149159
const tasks = [prepareJs(ctx)]

packages/integrations/src/gen-readme.js renamed to packages/integrations/src/gen-readme.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const mdtable = require('markdown-table')
2-
const fs = require('fs')
3-
const path = require('path')
1+
import mdtable from 'markdown-table'
2+
import fs from 'fs'
3+
import path from 'path'
44

5-
const integrations = require('./integration-list')
5+
import integrations from './integration-list'
66

77
const YES = ':white_check_mark:'
88
const NO = ':x:'
99

10-
const table = mdtable([
10+
const table: string = mdtable([
1111
['Name', 'iOS', 'Android', 'npm package'],
1212
...integrations
1313
.sort((a, b) => a.name.localeCompare(b.name))

packages/integrations/src/integration-list.js

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {parse} from 'yaml'
2+
import { readFileSync } from 'fs'
3+
import { resolve } from 'path'
4+
5+
export interface IntegrationDeclaration {
6+
name: string
7+
ios: {
8+
disabled?: boolean
9+
pod?: {
10+
name?: string
11+
version?: string
12+
}
13+
prefix?: string
14+
className?: string
15+
framework?: string
16+
header?: string
17+
}
18+
android: {
19+
disabled?: boolean
20+
factory?: {
21+
class?: string
22+
import?: string
23+
}
24+
maven?: {
25+
repo?: string
26+
name?: string
27+
version?: string
28+
}
29+
}
30+
}
31+
32+
export interface Integration extends IntegrationDeclaration {
33+
name: string
34+
slug(separator?: string): string
35+
npm: {
36+
package: string
37+
}
38+
}
39+
40+
const integrations: IntegrationDeclaration[] = parse(
41+
readFileSync(resolve(__dirname, '../integrations.yml'), 'utf-8')
42+
)
43+
44+
export default integrations
45+
.map(({ name, ios = {}, android = {} }) => {
46+
if (ios.disabled && android.disabled) {
47+
return null!
48+
}
49+
50+
const slug = (sep = '') => name.replace(/-|_| /g, sep)
51+
const suffix = ios.disabled ? '-android' : android.disabled ? '-ios' : ''
52+
53+
return {
54+
name,
55+
slug,
56+
ios,
57+
android,
58+
npm: {
59+
package: `@segment/analytics-react-native-${slug(
60+
'-'
61+
).toLowerCase()}${suffix}`
62+
}
63+
}
64+
})
65+
.filter(integration => integration !== null)

packages/integrations/src/module.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare module 'markdown-table'
2+
declare module '*.json'

packages/integrations/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"target": "es6",
5+
"outDir": "build",
6+
"allowSyntheticDefaultImports": true,
7+
"esModuleInterop": true
8+
},
9+
"include": ["src/**/*.ts"]
10+
}

tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"noImplicitAny": true,
5+
"target": "es5",
6+
"sourceMap": true,
7+
"declaration": true,
8+
"moduleResolution": "node"
9+
}
10+
}

0 commit comments

Comments
 (0)