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

Require necessary files #353

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions packages/build-tools/src/android/gradleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Android.Job>): Promise<void> {
Expand All @@ -31,7 +31,7 @@ function getEasBuildGradlePath(projectRoot: string): string {

async function createEasBuildGradle(projectRoot: string): Promise<void> {
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<void> {
Expand Down
10 changes: 5 additions & 5 deletions packages/build-tools/src/ios/fastfile.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -30,14 +28,16 @@ export async function createFastfileForResigningBuild({
}

await templateFile(
RESIGN_TEMPLATE_FILE_PATH,
RESIGN_TEMPLATE,
{
IPA_PATH: ipaPath,
SIGNING_IDENTITY: signingIdentity,
PROFILES,
KEYCHAIN_PATH: keychainPath,
},
outputFile,
{ mustache: false }
{
mustache: false,
}
);
}
13 changes: 4 additions & 9 deletions packages/build-tools/src/ios/gymfile.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -85,7 +80,7 @@ export async function createGymfileForSimulatorBuild({
}: SimulatorBuildOptions): Promise<void> {
await fs.mkdirp(logsDirectory);
await createGymfile({
template: SIMULATOR_TEMPLATE_FILE_PATH,
template: SIMULATOR_TEMPLATE,
outputFile,
vars: {
SCHEME: scheme,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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 }[] = [];
Expand All @@ -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,
Expand Down Expand Up @@ -238,11 +234,3 @@ async function createGymfile({
}): Promise<void> {
await templateFile(template, vars, outputFile, { mustache: false });
}

async function saveTemplateToTemporaryFileAsync(template: string): Promise<string> {
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;
}
6 changes: 3 additions & 3 deletions packages/build-tools/src/steps/functions/useNpmToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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');
Expand Down
14 changes: 4 additions & 10 deletions packages/build-tools/src/steps/utils/android/gradleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"';
Expand Down Expand Up @@ -68,7 +62,7 @@ function getEasBuildConfigureVersionGradlePath(workingDir: string): string {

async function createEasBuildInjectCredentialsGradle(workingDir: string): Promise<void> {
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(
Expand All @@ -77,7 +71,7 @@ async function createEasBuildConfigureVersionGradle(
): Promise<void> {
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,
Expand Down
6 changes: 3 additions & 3 deletions packages/build-tools/src/utils/npmrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Job>, logger: bunyan): Promise<void> {
if (ctx.env.NPM_TOKEN) {
Expand All @@ -24,10 +24,10 @@ async function createNpmrcIfNotExistsAsync(ctx: BuildContext<Job>, 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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/build-tools/src/utils/prepareBuildExecutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import fs from 'fs-extra';
import { BuildContext } from '../context';

export async function prepareExecutableAsync(ctx: BuildContext<Job>): Promise<void> {
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')
);
}
4 changes: 1 addition & 3 deletions packages/create-eas-build-function/src/templates.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -63,7 +61,7 @@ export async function copyTemplateAsync(
cwd: string;
}
): Promise<void> {
const modulePath = path.resolve(__dirname, '../templates', resolvedTemplate);
const modulePath = require(`../templates/${resolvedTemplate}`);
try {
await copyDir(modulePath, props.cwd);
} catch (error: any) {
Expand Down
12 changes: 6 additions & 6 deletions packages/template-file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
## API

```ts
templateFile(templateFilePath: string, outputFilePath: string, envs: Record<string, string | number>): Promise<void>
templateFile(templateContents: string, outputFilePath: string, envs: Record<string, string | number>): Promise<void>
```

## Usage example

```ts
import templateFile from '@expo/template-file';

await templateFile('abc.json.template', 'abc.json', { ABC: 123, XYZ: 789 });
```

`abc.json.template` file contents:
```
const templateContents = `
{
"someKey": {{ ABC }},
"anotherKey": {{ XYZ }}
}
`;

await templateFile(templateContents, 'abc.json', { ABC: 123, XYZ: 789 });
```

`abc.json` file should be created with the following contents:

```json
{
"someKey": 123,
Expand Down
10 changes: 6 additions & 4 deletions packages/template-file/src/__tests__/templateFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import fs from 'fs-extra';

import templateFile from '../templateFile';

const templatePath = path.join(__dirname, 'example.json.template');

describe('templateFile', () => {
const outputFile = path.join(os.tmpdir(), 'output.json');

Expand All @@ -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/);
});
});
3 changes: 1 addition & 2 deletions packages/template-file/src/templateFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import _ from 'lodash';
import fs from 'fs-extra';

async function templateFile(
templateFilePath: string,
templateString: string,
envs: Record<string, string | number | any>,
outputFilePath?: string,
{ mustache = true }: { mustache?: boolean } = {}
): Promise<string | void> {
const templateString = await fs.readFile(templateFilePath, 'utf8');
const compiledTemplate = _.template(
templateString,
mustache
Expand Down
Loading