Skip to content

Commit

Permalink
refactor(integration): port code-generator to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb committed Sep 25, 2018
1 parent c4f3a0a commit c229fb0
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 78 deletions.
9 changes: 2 additions & 7 deletions packages/core/tsconfig.json
@@ -1,13 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"strict": true,
"lib": ["es5", "es2015.promise", "dom"],
"target": "es5",

"sourceMap": true,
"declaration": true,
"outDir": "build",
"moduleResolution": "node"
"lib": ["es5", "es2015.promise", "dom"]
},
"include": ["src/**/*.ts"]
}
10 changes: 8 additions & 2 deletions packages/integrations/package.json
Expand Up @@ -4,8 +4,9 @@
"private": true,
"scripts": {
"clean": "rimraf build",
"generate:integrations": "node src/gen-integrations",
"generate:readme": "node src/gen-readme",
"ts": "ts-node -P tsconfig.json --files --pretty",
"generate:integrations": "yarn ts src/gen-integrations",
"generate:readme": "yarn ts src/gen-readme",
"generate": "run-p generate:*",
"build": "run-s clean generate"
},
Expand All @@ -14,5 +15,10 @@
"markdown-table": "^1.1.2",
"mustache": "^3.0.0",
"yaml": "^1.0.0-rc.7"
},
"devDependencies": {
"@types/mustache": "^0.8.31",
"@types/yaml": "^1.0.0",
"ts-node": "^7.0.1"
}
}
@@ -1,36 +1,44 @@
const fs = require('fs-extra')
const path = require('path')
const mustache = require('mustache')
const pkg = require('../package.json')
import fs from 'fs-extra'
import path from 'path'
import mustache from 'mustache'
import pkg from '../package.json'

import integrations, { Integration } from './integration-list'

interface Context extends Integration {
nativeModule: string
out: string
template: (template: string, view: {}, dest?: string) => Promise<void>
}

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

const safeWrite = async (file, contents) => {
const safeWrite = async (file: string, contents: string) => {
await fs.mkdirp(path.dirname(file))
await fs.writeFile(file, contents)
}

function prepareAndroid({ template, name, android, nativeModule, slug }) {
async function prepareAndroid({ template, name, android, nativeModule, slug }: Context) {
const identifier = slug('.').toLowerCase()
const classSlug = `${slug()}Integration`
const {
maven: {
repo,
repo = undefined,
name: depName = `com.segment.analytics.android.integrations:${slug(
'-'
).toLowerCase()}`,
version = '+@aar'
} = {},
factory: {
class: factoryClass = `${slug()}Integration`,
import: factoryImport = `com.segment.analytics.android.integrations.${identifier}.${factoryClass}`
class: factoryClass = classSlug,
import: factoryImport = `com.segment.analytics.android.integrations.${identifier}.${classSlug}`
} = {}
} = android
const classpath = `com.segment.analytics.reactnative.integration.${identifier}`
const dependency = `${depName}:${version}`
const root = 'android/src/main'

return Promise.all([
await Promise.all([
template('android/build.gradle', { dependency, maven: repo }),

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

function prepareiOS({ template, name, out, ios, nativeModule, slug }) {
async function prepareiOS({ template, name, out, ios, nativeModule, slug }: Context) {
const xcodeProject = 'ios/RNAnalyticsIntegration.xcodeproj'
const targetXcodeProject = `ios/${nativeModule}.xcodeproj`
const pod_name = `RNAnalyticsIntegration-${slug('-')}`
const {
pod: {
name: pod_dependency = `Segment-${slug()}`,
version: pod_version
version: pod_version = undefined
} = {},
prefix = 'SEG',
className = `${prefix}${slug()}IntegrationFactory`,
prefix = 'SEG'
} = ios
const classSlug = `${prefix}${slug()}IntegrationFactory`
const {
className = classSlug,
framework = pod_dependency,
header = className
header = classSlug
} = ios

return Promise.all([
await Promise.all([
fs.copy(
path.resolve(root, 'template', xcodeProject, 'project.xcworkspace'),
path.resolve(out, targetXcodeProject, 'project.xcworkspace')
Expand Down Expand Up @@ -90,7 +101,7 @@ function prepareiOS({ template, name, out, ios, nativeModule, slug }) {
])
}

function prepareJs({
async function prepareJs({
name,
npm,
nativeModule,
Expand All @@ -99,8 +110,8 @@ function prepareJs({
ios,
android,
slug
}) {
return Promise.all([
}: Context) {
await Promise.all([
safeWrite(
path.resolve(out, 'package.json'),
JSON.stringify(
Expand All @@ -124,26 +135,25 @@ function prepareJs({
])
}

function genIntegration({ name, ios, android, npm, slug }) {
function genIntegration({ name, ios, android, npm, slug }: Integration) {
const out = path.resolve(root, 'build', npm.package)
const nativeModule = `RNAnalyticsIntegration_${slug('_')}`
const template = async (template, view, dest = template) =>
safeWrite(
path.resolve(out, dest),
mustache.render(
await fs.readFile(path.resolve(root, 'template', template), 'utf-8'),
view
)
)
const ctx = {
const ctx: Context = {
name,
out,
nativeModule,
npm,
template,
ios,
android,
slug
slug,
template: async (template, view, dest = template) =>
await safeWrite(
path.resolve(out, dest),
mustache.render(
await fs.readFile(path.resolve(root, 'template', template), 'utf-8'),
view
)
)
}

const tasks = [prepareJs(ctx)]
Expand Down
@@ -1,13 +1,13 @@
const mdtable = require('markdown-table')
const fs = require('fs')
const path = require('path')
import mdtable from 'markdown-table'
import fs from 'fs'
import path from 'path'

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

const YES = ':white_check_mark:'
const NO = ':x:'

const table = mdtable([
const table: string = mdtable([
['Name', 'iOS', 'Android', 'npm package'],
...integrations
.sort((a, b) => a.name.localeCompare(b.name))
Expand Down
28 changes: 0 additions & 28 deletions packages/integrations/src/integration-list.js

This file was deleted.

65 changes: 65 additions & 0 deletions packages/integrations/src/integration-list.ts
@@ -0,0 +1,65 @@
import {parse} from 'yaml'
import { readFileSync } from 'fs'
import { resolve } from 'path'

export interface IntegrationDeclaration {
name: string
ios: {
disabled?: boolean
pod?: {
name?: string
version?: string
}
prefix?: string
className?: string
framework?: string
header?: string
}
android: {
disabled?: boolean
factory?: {
class?: string
import?: string
}
maven?: {
repo?: string
name?: string
version?: string
}
}
}

export interface Integration extends IntegrationDeclaration {
name: string
slug(separator?: string): string
npm: {
package: string
}
}

const integrations: IntegrationDeclaration[] = parse(
readFileSync(resolve(__dirname, '../integrations.yml'), 'utf-8')
)

export default integrations
.map(({ name, ios = {}, android = {} }) => {
if (ios.disabled && android.disabled) {
return null!
}

const slug = (sep = '') => name.replace(/-|_| /g, sep)
const suffix = ios.disabled ? '-android' : android.disabled ? '-ios' : ''

return {
name,
slug,
ios,
android,
npm: {
package: `@segment/analytics-react-native-${slug(
'-'
).toLowerCase()}${suffix}`
}
}
})
.filter(integration => integration !== null)
2 changes: 2 additions & 0 deletions packages/integrations/src/module.d.ts
@@ -0,0 +1,2 @@
declare module 'markdown-table'
declare module '*.json'
10 changes: 10 additions & 0 deletions packages/integrations/tsconfig.json
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "es6",
"outDir": "build",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"include": ["src/**/*.ts"]
}
10 changes: 10 additions & 0 deletions tsconfig.json
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"target": "es5",
"sourceMap": true,
"declaration": true,
"moduleResolution": "node"
}
}

0 comments on commit c229fb0

Please sign in to comment.