Skip to content

Commit

Permalink
🔨 Update sources to comply with TypeScript strictNullCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
belemaire authored and deepueg committed May 10, 2018
1 parent 14523ff commit 2e7d232
Show file tree
Hide file tree
Showing 26 changed files with 120 additions and 82 deletions.
14 changes: 10 additions & 4 deletions ern-api-gen/src/ApiGenUtils.ts
Expand Up @@ -17,7 +17,11 @@ export default class ApiGenUtils {
const generator = new DefaultGenerator().opts(opts)
const apis = generator.processPaths(generator.swagger.getPaths()).value

const result = []
const result: Array<{
apiName: string
requests: string[]
events: string[]
}> = []
for (const apiKey in apis) {
if (apis.hasOwnProperty(apiKey)) {
const {
Expand All @@ -33,9 +37,11 @@ export default class ApiGenUtils {
}
}

public static generateApiEventsAndRequestNames(api: any): any {
const requests = []
const events = []
public static generateApiEventsAndRequestNames(
api: any
): { requests: string[]; events: string[] } {
const requests: string[] = []
const events: string[] = []
for (const key in api) {
if (api.hasOwnProperty(key) && api[key].httpMethod === `EVENT`) {
events.push(api[key].camelizedNickName)
Expand Down
21 changes: 10 additions & 11 deletions ern-api-gen/src/normalizeConfig.ts
Expand Up @@ -52,26 +52,25 @@ export default function normalizeConfig({
packageName: string
rest?: any
}) {
let simpleName = name
let simpleName: string = name

if (isApiRe.test(name)) {
simpleName = isApiRe.exec(name).pop()
simpleName = isApiRe.exec(name)!.pop()!
}

const config: any = {}

if (simpleName) {
if (/^@/.test(simpleName)) {
const [
,
// tslint:disable-next-line:variable-name
_pkgName,
// tslint:disable-next-line:variable-name
_name,
] = /^@(.+?)\/(?:react-native-)?(.+?)(?:-api)?$/.exec(simpleName)
simpleName = _name
const reExec = /^@(.+?)\/(?:react-native-)?(.+?)(?:-api)?$/.exec(
simpleName
)
const pkgName = reExec![1]
const apiName = reExec![2]

simpleName = apiName
if (!namespace) {
namespace = _pkgName ? `com.${_pkgName}.${simpleName}` : simpleName
namespace = pkgName ? `com.${pkgName}.${simpleName}` : simpleName
}
}
config.moduleName = simpleName
Expand Down
Expand Up @@ -202,7 +202,7 @@ export default class ApiImplAndroidGenerator implements ApiImplGeneratable {
) {
log.debug('=== updating request handler implementation class ===')
try {
const editableFiles = []
const editableFiles: string[] = []
const {
outputDir,
resourceDir,
Expand Down
2 changes: 1 addition & 1 deletion ern-api-impl-gen/src/generators/ios/ApiImplIosGenerator.ts
Expand Up @@ -136,7 +136,7 @@ export default class ApiImplIosGenerator implements ApiImplGeneratable {
projectSpec,
iosProject
)
const editableFiles = []
const editableFiles: string[] = []
for (const api of apis) {
const { files, classNames } = ApiImplIosGenerator.getMustacheFileNamesMap(
resourceDir,
Expand Down
6 changes: 3 additions & 3 deletions ern-cauldron-api/src/CauldronHelper.ts
Expand Up @@ -155,17 +155,17 @@ export class CauldronHelper {
platformName: string
): Promise<string[]> {
const availableNativeApps = await this.getAllNativeApps()
const nativeAppsForGivenPlatform = []
const result: string[] = []
if (availableNativeApps) {
for (const nativeApp of availableNativeApps) {
for (const platform of nativeApp.platforms) {
if (platform.name === platformName) {
nativeAppsForGivenPlatform.push(nativeApp.name)
result.push(nativeApp.name)
}
}
}
}
return Promise.resolve(nativeAppsForGivenPlatform)
return result
}

public async getDescriptor(
Expand Down
11 changes: 7 additions & 4 deletions ern-container-gen/src/utils.ts
Expand Up @@ -234,7 +234,10 @@ export async function generateMiniAppsComposite(
shell.rm('-rf', path.join('node_modules', '**', '.babelrc'))

log.debug('Creating top level composite .babelrc')
const compositeBabelRc = { presets: ['react-native'], plugins: [] }
const compositeBabelRc: { plugins: string[]; presets: string[] } = {
plugins: [],
presets: ['react-native'],
}

// Ugly hacky way of handling module-resolver babel plugin
// At least it has some guarantees to make it safer but its just a temporary
Expand Down Expand Up @@ -270,7 +273,7 @@ export async function generateMiniAppsComposite(
if (compositeBabelRc.plugins.length === 0) {
// First MiniApp to add module-resolver plugin & config
// easy enough, we just copy over the plugin & config
compositeBabelRc.plugins.push(babelPlugin)
compositeBabelRc.plugins.push(<any>babelPlugin)
for (const x of babelPlugin) {
if (x instanceof Object && x.alias) {
moduleResolverAliases = x.alias
Expand Down Expand Up @@ -445,7 +448,7 @@ export function getPackageJsonDependenciesUsingMiniAppDeltas(
if (miniAppsDeltas.upgraded) {
for (const m of miniAppsDeltas.upgraded) {
const re = new RegExp(`\n${m.basePath}@(.+):`)
const initialVersion = re.exec(yarnlock)[1]
const initialVersion = re.exec(yarnlock)![1]
result[`${m.basePath}`] = initialVersion
}
}
Expand Down Expand Up @@ -474,7 +477,7 @@ export async function generatePluginsMustacheViews(
plugins: PackagePath[],
platform: string
) {
const pluginsViews = []
const pluginsViews: any[] = []
log.debug('Generating plugins mustache views')
for (const plugin of plugins) {
if (plugin.basePath === 'react-native') {
Expand Down
30 changes: 21 additions & 9 deletions ern-core/src/GitManifest.ts
Expand Up @@ -126,7 +126,7 @@ export default class GitManifest {
.filter(
d =>
ERN_VERSION_DIRECTORY_RE.test(d) &&
semver.lte(ERN_VERSION_DIRECTORY_RE.exec(d)[1], maxVersion)
semver.lte(ERN_VERSION_DIRECTORY_RE.exec(d)![1], maxVersion)
)
.map(d => path.join(this.repoAbsoluteLocalPath, 'plugins', d))
.value()
Expand All @@ -153,6 +153,12 @@ export default class GitManifest {
plugin: PackagePath,
platformVersion: string = Platform.currentVersion
): Promise<string | void> {
if (!plugin.version) {
throw new Error(
`Plugin ${PackagePath.toString()} does not have a version`
)
}

await this.syncIfNeeded()
const versionRe = /_v(.+)\+/
const scopeNameRe = /^(@.+)\/(.+)$/
Expand All @@ -168,7 +174,7 @@ export default class GitManifest {
platformVersion
)
.sort((a, b) =>
semver.compare(versionRe.exec(a)[1], versionRe.exec(b)[1])
semver.compare(versionRe.exec(a)![1], versionRe.exec(b)![1])
)
.reverse()

Expand All @@ -177,8 +183,8 @@ export default class GitManifest {
let pluginName
let basePluginPath
if (scopeNameRe.test(plugin.basePath)) {
pluginScope = scopeNameRe.exec(plugin.basePath)[1]
pluginName = scopeNameRe.exec(plugin.basePath)[2]
pluginScope = scopeNameRe.exec(plugin.basePath)![1]
pluginName = scopeNameRe.exec(plugin.basePath)![2]
basePluginPath = path.join(pluginsConfigurationDirectory, pluginScope)
} else {
pluginName = plugin.basePath
Expand All @@ -195,12 +201,12 @@ export default class GitManifest {

const pluginVersions = _.map(
pluginConfigDirectories,
s => versionRe.exec(s)[1]
s => versionRe.exec(s)![1]
)

const matchingVersion = _.find(
pluginVersions.sort(semver.compare).reverse(),
d => plugin.version >= d
d => semver.gte(plugin.version!, d)
)
if (matchingVersion) {
let pluginConfigurationPath = ''
Expand All @@ -226,13 +232,19 @@ export default class GitManifest {
plugin: PackagePath,
platformVersion: string = Platform.currentVersion
): Promise<string | void> {
if (!plugin.version) {
throw new Error(
`Plugin ${PackagePath.toString()} does not have a version`
)
}

await this.syncIfNeeded()
const versionRe = /_v(.+)\+/
const orderedPluginsConfigurationDirectories = this.getPluginsConfigurationDirectories(
platformVersion
)
.sort((a, b) =>
semver.compare(versionRe.exec(a)[1], versionRe.exec(b)[1])
semver.compare(versionRe.exec(a)![1], versionRe.exec(b)![1])
)
.reverse()

Expand All @@ -244,12 +256,12 @@ export default class GitManifest {
fs
.readdirSync(pluginsConfigurationDirectory)
.filter(f => f.startsWith(pluginScopeAndName)),
s => versionRe.exec(s)[1]
s => versionRe.exec(s)![1]
)

const matchingVersion = _.find(
pluginVersions.sort(semver.compare).reverse(),
d => plugin.version >= d
d => semver.gte(plugin.version!, d)
)
if (matchingVersion) {
const pluginConfigurationPath = path.join(
Expand Down
4 changes: 2 additions & 2 deletions ern-core/src/Manifest.ts
Expand Up @@ -273,8 +273,8 @@ export class Manifest {
if (!conf.origin) {
if (npmScopeModuleRe.test(plugin.basePath)) {
conf.origin = {
name: `${npmScopeModuleRe.exec(`${plugin.basePath}`)[2]}`,
scope: `${npmScopeModuleRe.exec(`${plugin.basePath}`)[1]}`,
name: `${npmScopeModuleRe.exec(plugin.basePath)![2]}`,
scope: `${npmScopeModuleRe.exec(plugin.basePath)![1]}`,
type: 'npm',
version: plugin.version,
}
Expand Down
2 changes: 1 addition & 1 deletion ern-core/src/MiniApp.ts
Expand Up @@ -481,7 +481,7 @@ with "ern" : { "version" : "${this.packageJson.ernPlatformVersion}" } instead`)
public getUnscopedModuleName(moduleName: string): string {
const npmScopeModuleRe = /(@.*)\/(.*)/
return npmScopeModuleRe.test(moduleName)
? npmScopeModuleRe.exec(`${moduleName}`)[2]
? npmScopeModuleRe.exec(moduleName)![2]
: moduleName
}

Expand Down
16 changes: 8 additions & 8 deletions ern-core/src/PackagePath.ts
Expand Up @@ -56,18 +56,18 @@ export class PackagePath {
if (GIT_SSH_PATH_RE.test(path) || GIT_HTTPS_PATH_RE.test(path)) {
this.basePath = path
} else if (GIT_SSH_PATH_VERSION_RE.test(path)) {
this.basePath = GIT_SSH_PATH_VERSION_RE.exec(path)[1]
this.version = GIT_SSH_PATH_VERSION_RE.exec(path)[2]
this.basePath = GIT_SSH_PATH_VERSION_RE.exec(path)![1]
this.version = GIT_SSH_PATH_VERSION_RE.exec(path)![2]
} else if (GIT_HTTPS_PATH_VERSION_RE.test(path)) {
this.basePath = GIT_HTTPS_PATH_VERSION_RE.exec(path)[1]
this.version = GIT_HTTPS_PATH_VERSION_RE.exec(path)[2]
this.basePath = GIT_HTTPS_PATH_VERSION_RE.exec(path)![1]
this.version = GIT_HTTPS_PATH_VERSION_RE.exec(path)![2]
} else if (REGISTRY_PATH_VERSION_RE.test(path)) {
this.basePath = REGISTRY_PATH_VERSION_RE.exec(path)[1]
this.version = REGISTRY_PATH_VERSION_RE.exec(path)[2]
this.basePath = REGISTRY_PATH_VERSION_RE.exec(path)![1]
this.version = REGISTRY_PATH_VERSION_RE.exec(path)![2]
} else if (FILE_PATH_WITH_PREFIX_RE.test(path)) {
this.basePath = FILE_PATH_WITH_PREFIX_RE.exec(path)[1]
this.basePath = FILE_PATH_WITH_PREFIX_RE.exec(path)![1]
} else if (FILE_PATH_WITHOUT_PREFIX_RE.test(path)) {
this.basePath = FILE_PATH_WITHOUT_PREFIX_RE.exec(path)[1]
this.basePath = FILE_PATH_WITHOUT_PREFIX_RE.exec(path)![1]
} else {
this.basePath = path
}
Expand Down
2 changes: 1 addition & 1 deletion ern-core/src/childProcess.ts
Expand Up @@ -47,7 +47,7 @@ export async function execp(
if (error) {
reject(error)
} else {
resolve(stdout)
resolve(stdout.toString())
}
})
cp.stdout.on('data', data => log.trace(data.toString()))
Expand Down
2 changes: 1 addition & 1 deletion ern-core/src/dependencyLookup.ts
Expand Up @@ -7,7 +7,7 @@ export async function getMiniAppsUsingNativeDependency(
miniAppsPaths: PackagePath[],
nativeDependency: PackagePath
): Promise<MiniApp[]> {
const result = []
const result: MiniApp[] = []
const nativeDependencyString = nativeDependency.basePath
for (const miniAppPath of miniAppsPaths) {
const miniApp = await spin(
Expand Down
2 changes: 1 addition & 1 deletion ern-core/src/ios.ts
Expand Up @@ -105,7 +105,7 @@ export function parseIOSDevicesList(
const devicePattern = /(.*?) \((.*?)\) \[(.*?)\]/
const noSimulatorPattern = /(.*?) \((.*?)\) \[(.*?)\] \((.*?)\)/

return text.split('\n').reduce((list, line) => {
return text.split('\n').reduce((list: any, line: string) => {
const device = line.match(devicePattern)
if (
device &&
Expand Down
18 changes: 10 additions & 8 deletions ern-core/src/nativeDependenciesLookup.ts
Expand Up @@ -37,12 +37,14 @@ export async function findNativeDependencies(
const r = /^win/.test(process.platform)
? /^(@.*?\\.*?)\\|^(.*?)\\/.exec(nativeDepsDirectory)
: /^(@.*?\/.*?)\/|^(.*?)\//.exec(nativeDepsDirectory)
if (r[1]) {
nativeDependenciesNames.add(
/^win/.test(process.platform) ? r[1].replace('\\', '/') : r[1]
)
} else {
nativeDependenciesNames.add(r[2])
if (r) {
if (r[1]) {
nativeDependenciesNames.add(
/^win/.test(process.platform) ? r[1].replace('\\', '/') : r[1]
)
} else {
nativeDependenciesNames.add(r[2])
}
}
}

Expand All @@ -63,11 +65,11 @@ export async function findNativeDependencies(
)
const nativeDepPackageJson = require(pathToNativeDependencyPackageJson)
const name = NPM_SCOPED_MODULE_RE.test(nativeDependencyName)
? NPM_SCOPED_MODULE_RE.exec(nativeDependencyName)[2]
? NPM_SCOPED_MODULE_RE.exec(nativeDependencyName)![2]
: nativeDependencyName
const scope =
(NPM_SCOPED_MODULE_RE.test(nativeDependencyName) &&
NPM_SCOPED_MODULE_RE.exec(nativeDependencyName)[1]) ||
NPM_SCOPED_MODULE_RE.exec(nativeDependencyName)![1]) ||
undefined
const version = getUnprefixedVersion(nativeDepPackageJson.version)
const dep = packagePathFrom(name, { scope, version })
Expand Down
4 changes: 2 additions & 2 deletions ern-core/src/resolveNativeDependenciesVersions.ts
Expand Up @@ -49,7 +49,7 @@ export function resolveNativeDependenciesVersionsGivenMismatchLevel(
resolved: PackagePath[]
pluginsWithMismatchingVersions: string[]
} {
const result = {
const result: any = {
pluginsWithMismatchingVersions: [],
resolved: [],
}
Expand Down Expand Up @@ -175,7 +175,7 @@ export function resolveNativeDependenciesVersions(
export async function resolveNativeDependenciesVersionsOfMiniApps(
miniapps: MiniApp[]
): Promise<any> {
const nativeDependenciesArray = []
const nativeDependenciesArray: NativeDependencies[] = []
for (const miniapp of miniapps) {
const miniappNativeDependencies = await miniapp.getNativeDependencies()
nativeDependenciesArray.push(miniappNativeDependencies)
Expand Down
4 changes: 2 additions & 2 deletions ern-core/src/utils.ts
Expand Up @@ -337,8 +337,8 @@ export function getDownloadedPluginPath(pluginOrigin: any) {
downloadPath = path.join('node_modules', pluginOrigin.name)
}
} else if (pluginOrigin.type === 'git') {
if (pluginOrigin.version) {
downloadPath = gitDirectoryRe.exec(`${pluginOrigin.url}`)[1]
if (pluginOrigin.version && gitDirectoryRe.test(pluginOrigin.url)) {
downloadPath = gitDirectoryRe.exec(pluginOrigin.url)![1]
}
}

Expand Down
4 changes: 2 additions & 2 deletions ern-local-cli/src/commands/cauldron/add/miniapps.ts
Expand Up @@ -88,7 +88,7 @@ export const handler = async ({
},
})

const miniAppsObjs = []
const miniAppsObjs: MiniApp[] = []
const miniAppsDependencyPaths = _.map(miniapps, m =>
PackagePath.fromString(m)
)
Expand All @@ -104,7 +104,7 @@ export const handler = async ({
const miniAppsInCauldron = await cauldron.getContainerMiniApps(
napDescriptor
)
const miniAppsInCauldronObjs = []
const miniAppsInCauldronObjs: MiniApp[] = []
for (const miniAppInCauldron of miniAppsInCauldron) {
const m = await spin(
`Retrieving ${miniAppInCauldron.toString()} MiniApp`,
Expand Down

0 comments on commit 2e7d232

Please sign in to comment.