Skip to content

Commit

Permalink
refactor: emphasize bundled @expo/* packages and limit imports (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric authored Sep 3, 2023
1 parent b4dec03 commit 0bd601d
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/__tests__/commands/code-provider.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import * as json from 'jsonc-parser';
import * as jsonc from 'jsonc-parser';
import { commands, TextEditor, window } from 'vscode';

import { PreviewCommand, PreviewModProvider } from '../../preview/constants';
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('CodeProvider', () => {
);

const preview = await waitForEditorOpen('AndroidManifest.xml');
const addition = json.modify(
const addition = jsonc.modify(
app.document.getText(),
['expo', 'updates', 'url'],
'https://example.com/updates/url',
Expand All @@ -43,7 +43,7 @@ describe('CodeProvider', () => {
const EXPECTED_UPDATES_URL =
'<meta-data android:name="expo.modules.updates.EXPO_UPDATE_URL" android:value="https://example.com/updates/url"/>';

await replaceEditorContent(app, json.applyEdits(app.document.getText(), addition));
await replaceEditorContent(app, jsonc.applyEdits(app.document.getText(), addition));
await app.document.save();

const includesChange = await waitForTrue(
Expand All @@ -52,11 +52,11 @@ describe('CodeProvider', () => {

expect(includesChange).to.equal(true);

const removal = json.modify(app.document.getText(), ['expo', 'updates'], undefined, {
const removal = jsonc.modify(app.document.getText(), ['expo', 'updates'], undefined, {
formattingOptions: { insertSpaces: true },
});

await replaceEditorContent(app, json.applyEdits(app.document.getText(), removal));
await replaceEditorContent(app, jsonc.applyEdits(app.document.getText(), removal));
await app.document.save();

const excludesChange = await waitForFalse(
Expand Down
8 changes: 4 additions & 4 deletions src/expo/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
resolveConfigPluginFunction,
resolveConfigPluginFunctionWithInfo,
} from '@expo/config-plugins/build/utils/plugin-resolver';
import { findNodeAtLocation, getNodeValue, Node, Range } from 'jsonc-parser';

import { ExpoProject } from './project';
import {
resolveConfigPluginFunction,
resolveConfigPluginFunctionWithInfo,
} from '../packages/config-plugins';
import { truthy } from '../utils/array';
import { resetModuleFrom } from '../utils/module';

Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as vscode from 'vscode';
import vscode from 'vscode';

import { ExpoProjectCache } from './expo/project';
import { ExpoDebuggersProvider } from './expoDebuggers';
Expand Down
40 changes: 40 additions & 0 deletions src/packages/config-plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-disable import/first,import/order */

/**
* Reuse the `ModPlatform` type from the Config Plugins package.
* This outlines the platforms we can expect from the Config Plugins.
*/
export type { ModPlatform } from '@expo/config-plugins/build/Plugin.types';

/**
* Use a bundled version of the Gradle properties formatter.
* This is used when previewing the project's Gradle properties.
* The library itself likely won't change much, so it's safe to only rely on the bundled version.
*/
export { propertiesListToString as formatGradleProperties } from '@expo/config-plugins/build/android/Properties';

/**
* Use a bundled version of the XML formatter.
* This is used when previewing XML files within the project.
* The library itself likely won't change much, so it's safe to only rely on the bundled version.
*/
export { format as formatXml } from '@expo/config-plugins/build/utils/XML';

/**
* Use a bundled version of the Config Plugins mod compiler.
* This is used to "compile" or run the plugins on a manifest.
*
* @note This bundled package is slightly outdated, attempt to load from `npx expo config` instead.
*/
export { compileModsAsync } from '@expo/config-plugins/build/plugins/mod-compiler';

/**
* Use a bundled version of the Config Plugins resolver.
* This is used when validating the project's app manifest.
*
* @note This bundled package is slightly outdated and should be loaded from the project where possible.
*/
export {
resolveConfigPluginFunction,
resolveConfigPluginFunctionWithInfo,
} from '@expo/config-plugins/build/utils/plugin-resolver';
9 changes: 9 additions & 0 deletions src/packages/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable import/first,import/order */

/**
* Use a bundled version of the Expo Config package.
* This is used to retrieve the app manifest from a project.
*
* @note This bundled package is slightly outdated, attempt to load from `npx expo config` instead.
*/
export { type ExpoConfig, getConfig } from '@expo/config/build/Config';
9 changes: 9 additions & 0 deletions src/packages/plist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable import/first,import/order */

/**
* Use a bundled version of the plist formatter.
* This is used when previewing iOS plist files within the project.
* The library itself likely won't change much, so it's safe to only rely on the bundled version.
*/
import plist from '@expo/plist';
export const formatPlist = plist.build;
9 changes: 9 additions & 0 deletions src/packages/prebuild-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable import/first,import/order */

/**
* Use a bundled version of the prebuild config generator.
* This is used to retrieve the config for Config Plugins.
*
* @note This bundled package is slightly outdated, attempt to load from `npx expo config` instead.
*/
export { getPrebuildConfigAsync } from '@expo/prebuild-config/build/getPrebuildConfig';
14 changes: 7 additions & 7 deletions src/preview/CodeProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AndroidConfig, XML } from '@expo/config-plugins';
import plist from '@expo/plist';
import * as path from 'path';
import * as vscode from 'vscode';
import path from 'path';
import vscode from 'vscode';

import { getProjectRoot } from '../expo/project';
import { formatGradleProperties, formatXml } from '../packages/config-plugins';
import { formatPlist } from '../packages/plist';
import { debug } from '../utils/debug';
import { resetModulesFrom } from '../utils/module';

Expand Down Expand Up @@ -114,12 +114,12 @@ export class CodeProvider implements vscode.TextDocumentContentProvider {
case 'json':
return JSON.stringify(results, null, 2);
case 'xml':
return XML.format(results);
return formatXml(results);
case 'plist':
case 'entitlements':
return plist.build(results);
return formatPlist(results);
case 'properties':
return AndroidConfig.Properties.propertiesListToString(results);
return formatGradleProperties(results);
default:
throw new Error('Unknown language: ' + language);
}
Expand Down
8 changes: 4 additions & 4 deletions src/preview/ExpoConfigCodeProvider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getConfig } from '@expo/config';
import { compileModsAsync } from '@expo/config-plugins';
import { getPrebuildConfigAsync } from '@expo/prebuild-config';
import * as vscode from 'vscode';
import vscode from 'vscode';

import { CodeProvider, BasicCodeProviderOptions, CodeProviderLanguage } from './CodeProvider';
import { ExpoConfigType } from './constants';
import { getConfig } from '../packages/config';
import { compileModsAsync } from '../packages/config-plugins';
import { getPrebuildConfigAsync } from '../packages/prebuild-config';

export class ExpoConfigCodeProvider extends CodeProvider {
readonly defaultLanguage: CodeProviderLanguage = 'json';
Expand Down
6 changes: 3 additions & 3 deletions src/preview/IntrospectCodeProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { compileModsAsync, ModPlatform } from '@expo/config-plugins';
import { getPrebuildConfigAsync } from '@expo/prebuild-config';
import assert from 'assert';
import * as vscode from 'vscode';
import vscode from 'vscode';

import { BasicCodeProviderOptions, CodeProvider, CodeProviderLanguage } from './CodeProvider';
import { compileModsAsync, type ModPlatform } from '../packages/config-plugins';
import { getPrebuildConfigAsync } from '../packages/prebuild-config';

class IntrospectCodeProvider extends CodeProvider {
getModName(): string {
Expand Down
2 changes: 1 addition & 1 deletion src/preview/setupPreview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as vscode from 'vscode';
import vscode from 'vscode';

import { CodeProvider } from './CodeProvider';
import {
Expand Down

0 comments on commit 0bd601d

Please sign in to comment.