diff --git a/packages/build-tools/src/android/gradleConfig.ts b/packages/build-tools/src/android/gradleConfig.ts index 573cad4f..2c93f8bc 100644 --- a/packages/build-tools/src/android/gradleConfig.ts +++ b/packages/build-tools/src/android/gradleConfig.ts @@ -6,7 +6,7 @@ import fs from 'fs-extra'; import { BuildContext } from '../context'; -const EAS_BUILD_GRADLE_TEMPLATE_PATH = path.join(__dirname, '../../templates/eas-build.gradle'); +const EAS_BUILD_GRADLE_TEMPLATE = require('../../templates/eas-build.gradle'); const APPLY_EAS_BUILD_GRADLE_LINE = 'apply from: "./eas-build.gradle"'; export async function configureBuildGradle(ctx: BuildContext): Promise { @@ -31,7 +31,7 @@ function getEasBuildGradlePath(projectRoot: string): string { async function createEasBuildGradle(projectRoot: string): Promise { const easBuildGradlePath = getEasBuildGradlePath(projectRoot); - await fs.copy(EAS_BUILD_GRADLE_TEMPLATE_PATH, easBuildGradlePath); + await fs.writeFile(easBuildGradlePath, EAS_BUILD_GRADLE_TEMPLATE); } async function addApplyToBuildGradle(projectRoot: string): Promise { diff --git a/packages/build-tools/src/ios/fastfile.ts b/packages/build-tools/src/ios/fastfile.ts index 28b95a9b..f490c8b8 100644 --- a/packages/build-tools/src/ios/fastfile.ts +++ b/packages/build-tools/src/ios/fastfile.ts @@ -1,10 +1,8 @@ -import path from 'path'; - import templateFile from '@expo/template-file'; import { TargetProvisioningProfiles } from './credentials/manager'; -const RESIGN_TEMPLATE_FILE_PATH = path.join(__dirname, '../../templates/Fastfile.resign.template'); +const RESIGN_TEMPLATE = require('../../templates/Fastfile.resign.template'); export async function createFastfileForResigningBuild({ outputFile, @@ -30,7 +28,7 @@ export async function createFastfileForResigningBuild({ } await templateFile( - RESIGN_TEMPLATE_FILE_PATH, + RESIGN_TEMPLATE, { IPA_PATH: ipaPath, SIGNING_IDENTITY: signingIdentity, @@ -38,6 +36,8 @@ export async function createFastfileForResigningBuild({ KEYCHAIN_PATH: keychainPath, }, outputFile, - { mustache: false } + { + mustache: false, + } ); } diff --git a/packages/build-tools/src/ios/gymfile.ts b/packages/build-tools/src/ios/gymfile.ts index e9056513..1f43a748 100644 --- a/packages/build-tools/src/ios/gymfile.ts +++ b/packages/build-tools/src/ios/gymfile.ts @@ -1,15 +1,10 @@ -import path from 'path'; - import templateFile from '@expo/template-file'; import fs from 'fs-extra'; import { Credentials } from './credentials/manager'; -const ARCHIVE_TEMPLATE_FILE_PATH = path.join(__dirname, '../../templates/Gymfile.archive.template'); -const SIMULATOR_TEMPLATE_FILE_PATH = path.join( - __dirname, - '../../templates/Gymfile.simulator.template' -); +const ARCHIVE_TEMPLATE = require('../../templates/Gymfile.archive.template'); +const SIMULATOR_TEMPLATE = require('../../templates/Gymfile.simulator.template'); interface ArchiveBuildOptions { outputFile: string; @@ -58,7 +53,7 @@ export async function createGymfileForArchiveBuild({ await fs.mkdirp(logsDirectory); await createGymfile({ - template: ARCHIVE_TEMPLATE_FILE_PATH, + template: ARCHIVE_TEMPLATE, outputFile, vars: { KEYCHAIN_PATH: credentials.keychainPath, @@ -85,7 +80,7 @@ export async function createGymfileForSimulatorBuild({ }: SimulatorBuildOptions): Promise { await fs.mkdirp(logsDirectory); await createGymfile({ - template: SIMULATOR_TEMPLATE_FILE_PATH, + template: SIMULATOR_TEMPLATE, outputFile, vars: { SCHEME: scheme, diff --git a/packages/build-tools/src/steps/functions/generateGymfileFromTemplate.ts b/packages/build-tools/src/steps/functions/generateGymfileFromTemplate.ts index b21e5fee..00958518 100644 --- a/packages/build-tools/src/steps/functions/generateGymfileFromTemplate.ts +++ b/packages/build-tools/src/steps/functions/generateGymfileFromTemplate.ts @@ -1,6 +1,5 @@ import assert from 'assert'; import path from 'path'; -import os from 'os'; import fs from 'fs-extra'; import { BuildFunction, BuildStepInput, BuildStepInputValueTypeName } from '@expo/steps'; @@ -9,7 +8,6 @@ import { IOSConfig } from '@expo/config-plugins'; import plist from '@expo/plist'; import { bunyan } from '@expo/logger'; import templateFile from '@expo/template-file'; -import { v4 as uuid } from 'uuid'; import { IosBuildCredentialsSchema } from '../utils/ios/credentials/credentials'; import IosCredentialsManager, { Credentials } from '../utils/ios/credentials/manager'; @@ -147,8 +145,6 @@ export function generateGymfileFromTemplateFunction(): BuildFunction { buildConfiguration ); - const templatePath = await saveTemplateToTemporaryFileAsync(template); - const gymfilePath = path.join(stepCtx.workingDirectory, 'ios/Gymfile'); const PROFILES: { BUNDLE_ID: string; UUID: string }[] = []; @@ -175,7 +171,7 @@ export function generateGymfileFromTemplateFunction(): BuildFunction { const simulatorDestination = `generic/platform=${isTV ? 'tvOS' : 'iOS'} Simulator`; await createGymfile({ - template: templatePath, + template, outputFile: gymfilePath, vars: { SCHEME: scheme, @@ -238,11 +234,3 @@ async function createGymfile({ }): Promise { await templateFile(template, vars, outputFile, { mustache: false }); } - -async function saveTemplateToTemporaryFileAsync(template: string): Promise { - const directory = path.join(os.tmpdir(), `gymfile-template-${uuid()}`); - await fs.mkdir(directory, { recursive: true }); - const templatePath = path.join(directory, 'Gymfile.template'); - await fs.writeFile(templatePath, template); - return templatePath; -} diff --git a/packages/build-tools/src/steps/functions/useNpmToken.ts b/packages/build-tools/src/steps/functions/useNpmToken.ts index 9516c32b..ad99ef79 100644 --- a/packages/build-tools/src/steps/functions/useNpmToken.ts +++ b/packages/build-tools/src/steps/functions/useNpmToken.ts @@ -5,7 +5,7 @@ import { BuildFunction } from '@expo/steps'; import { findPackagerRootDir } from '../../utils/packageManager'; -const NPMRC_TEMPLATE_PATH = path.join(__dirname, '../../templates/npmrc'); +const NPMRC_TEMPLATE = require('../../../templates/npmrc'); export function createSetUpNpmrcBuildFunction(): BuildFunction { return new BuildFunction({ @@ -20,10 +20,10 @@ export function createSetUpNpmrcBuildFunction(): BuildFunction { if (await fs.pathExists(projectNpmrcPath)) { logger.info('.npmrc already exists in your project directory, skipping generation'); } else { - const npmrcContents = await fs.readFile(NPMRC_TEMPLATE_PATH, 'utf8'); + const npmrcContents = NPMRC_TEMPLATE; logger.info('Creating .npmrc in your project directory with the following contents:'); logger.info(npmrcContents); - await fs.copy(NPMRC_TEMPLATE_PATH, projectNpmrcPath); + await fs.writeFile(projectNpmrcPath, npmrcContents); } } else { const projectNpmrcPath = path.join(findPackagerRootDir(stepCtx.workingDirectory), '.npmrc'); diff --git a/packages/build-tools/src/steps/utils/android/gradleConfig.ts b/packages/build-tools/src/steps/utils/android/gradleConfig.ts index 40b389ae..271130f8 100644 --- a/packages/build-tools/src/steps/utils/android/gradleConfig.ts +++ b/packages/build-tools/src/steps/utils/android/gradleConfig.ts @@ -5,14 +5,8 @@ import { bunyan } from '@expo/logger'; import fs from 'fs-extra'; import templateFile from '@expo/template-file'; -const EAS_BUILD_INJECT_CREDENTIALS_GRADLE_TEMPLATE_PATH = path.join( - __dirname, - '../../../../templates/eas-build-inject-android-credentials.gradle' -); -const EAS_BUILD_CONFIGURE_VERSION_GRADLE_TEMPLATE_PATH = path.join( - __dirname, - '../../../../templates/eas-build-configure-version.gradle.template' -); +const EAS_BUILD_INJECT_CREDENTIALS_GRADLE_TEMPLATE = require('../../../../templates/eas-build-inject-android-credentials.gradle'); +const EAS_BUILD_CONFIGURE_VERSION_GRADLE_TEMPLATE = require('../../../../templates/eas-build-configure-version.gradle.template'); const APPLY_EAS_BUILD_INJECT_CREDENTIALS_GRADLE_LINE = 'apply from: "./eas-build-inject-android-credentials.gradle"'; @@ -68,7 +62,7 @@ function getEasBuildConfigureVersionGradlePath(workingDir: string): string { async function createEasBuildInjectCredentialsGradle(workingDir: string): Promise { const targetPath = getEasBuildInjectCredentialsGradlePath(workingDir); - await fs.copy(EAS_BUILD_INJECT_CREDENTIALS_GRADLE_TEMPLATE_PATH, targetPath); + await fs.writeFile(targetPath, EAS_BUILD_INJECT_CREDENTIALS_GRADLE_TEMPLATE); } async function createEasBuildConfigureVersionGradle( @@ -77,7 +71,7 @@ async function createEasBuildConfigureVersionGradle( ): Promise { const targetPath = getEasBuildConfigureVersionGradlePath(workingDir); await templateFile( - EAS_BUILD_CONFIGURE_VERSION_GRADLE_TEMPLATE_PATH, + EAS_BUILD_CONFIGURE_VERSION_GRADLE_TEMPLATE, { VERSION_CODE: versionCode, VERSION_NAME: versionName, diff --git a/packages/build-tools/src/utils/npmrc.ts b/packages/build-tools/src/utils/npmrc.ts index 062f5a38..2efd4b72 100644 --- a/packages/build-tools/src/utils/npmrc.ts +++ b/packages/build-tools/src/utils/npmrc.ts @@ -8,7 +8,7 @@ import { BuildContext } from '../context'; import { findPackagerRootDir } from './packageManager'; -const NPMRC_TEMPLATE_PATH = path.join(__dirname, '../../templates/npmrc'); +const NPMRC_TEMPLATE = require('../../templates/npmrc'); export async function setUpNpmrcAsync(ctx: BuildContext, logger: bunyan): Promise { if (ctx.env.NPM_TOKEN) { @@ -24,10 +24,10 @@ async function createNpmrcIfNotExistsAsync(ctx: BuildContext, logger: bunya if (await fs.pathExists(projectNpmrcPath)) { logger.info('.npmrc already exists in your project directory, skipping generation'); } else { - const npmrcContents = await fs.readFile(NPMRC_TEMPLATE_PATH, 'utf8'); + const npmrcContents = NPMRC_TEMPLATE; logger.info('Creating .npmrc in your project directory with the following contents:'); logger.info(npmrcContents); - await fs.copy(NPMRC_TEMPLATE_PATH, projectNpmrcPath); + await fs.writeFile(projectNpmrcPath, NPMRC_TEMPLATE); } } diff --git a/packages/build-tools/src/utils/prepareBuildExecutable.ts b/packages/build-tools/src/utils/prepareBuildExecutable.ts index 8013cbbf..d87b49f1 100644 --- a/packages/build-tools/src/utils/prepareBuildExecutable.ts +++ b/packages/build-tools/src/utils/prepareBuildExecutable.ts @@ -6,8 +6,8 @@ import fs from 'fs-extra'; import { BuildContext } from '../context'; export async function prepareExecutableAsync(ctx: BuildContext): Promise { - await fs.copy( - path.join(__dirname, '../../bin/set-env'), - path.join(ctx.buildExecutablesDirectory, 'set-env') + await fs.writeFile( + path.join(ctx.buildExecutablesDirectory, 'set-env'), + require('../../bin/set-env') ); } diff --git a/packages/create-eas-build-function/src/templates.ts b/packages/create-eas-build-function/src/templates.ts index 76c53fe7..65b9a239 100644 --- a/packages/create-eas-build-function/src/templates.ts +++ b/packages/create-eas-build-function/src/templates.ts @@ -1,7 +1,5 @@ #!/usr/bin/env node -import path from 'path'; - import fs from 'fs-extra'; import chalk from 'chalk'; import prompts from 'prompts'; @@ -63,7 +61,7 @@ export async function copyTemplateAsync( cwd: string; } ): Promise { - const modulePath = path.resolve(__dirname, '../templates', resolvedTemplate); + const modulePath = require(`../templates/${resolvedTemplate}`); try { await copyDir(modulePath, props.cwd); } catch (error: any) { diff --git a/packages/template-file/README.md b/packages/template-file/README.md index df262f6f..bc4878bd 100644 --- a/packages/template-file/README.md +++ b/packages/template-file/README.md @@ -5,7 +5,7 @@ ## API ```ts -templateFile(templateFilePath: string, outputFilePath: string, envs: Record): Promise +templateFile(templateContents: string, outputFilePath: string, envs: Record): Promise ``` ## Usage example @@ -13,18 +13,18 @@ templateFile(templateFilePath: string, outputFilePath: string, envs: Record { const outputFile = path.join(os.tmpdir(), 'output.json'); @@ -15,14 +13,18 @@ describe('templateFile', () => { }); it('should create an output file with the filled-out template', async () => { - await templateFile(templatePath, { SOME_KEY: 123, ANOTHER_KEY: 456 }, outputFile); + await templateFile( + require('./example.json.template'), + { SOME_KEY: 123, ANOTHER_KEY: 456 }, + outputFile + ); const outputFileContents = await fs.readFile(outputFile, 'utf8'); const outputFileJson = JSON.parse(outputFileContents); expect(outputFileJson).toEqual({ someKey: 123, anotherKey: 456 }); }); it('should throw an error if some variables are missing', async () => { - const templateFilePromise = templateFile(templatePath, {}, outputFile); + const templateFilePromise = templateFile(require('./example.json.template'), {}, outputFile); await expect(templateFilePromise).rejects.toThrow(/is not defined/); }); }); diff --git a/packages/template-file/src/templateFile.ts b/packages/template-file/src/templateFile.ts index dd3e0634..022ece9a 100644 --- a/packages/template-file/src/templateFile.ts +++ b/packages/template-file/src/templateFile.ts @@ -2,12 +2,11 @@ import _ from 'lodash'; import fs from 'fs-extra'; async function templateFile( - templateFilePath: string, + templateString: string, envs: Record, outputFilePath?: string, { mustache = true }: { mustache?: boolean } = {} ): Promise { - const templateString = await fs.readFile(templateFilePath, 'utf8'); const compiledTemplate = _.template( templateString, mustache