Skip to content

Commit

Permalink
chore: lint root files with prettier and eslint (#125)
Browse files Browse the repository at this point in the history
* chore: hook up Prettier, lint root files, reformat

* fix prettier checks on Windows

* remove redundant string
  • Loading branch information
Simek authored Nov 16, 2021
1 parent 8a7306a commit 1cfa7bb
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 81 deletions.
16 changes: 5 additions & 11 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
module.exports = {
ignorePatterns: ['/build/**', 'node_modules/**'],
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
'jest',
],
plugins: ['@typescript-eslint', 'jest', 'prettier'],
env: {
es6: true,
'jest/globals': true,
node: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
rules: {
'@typescript-eslint/explicit-member-accessibility': [
'error', { 'accessibility': 'no-public' },
],
'@typescript-eslint/explicit-member-accessibility': ['error', { accessibility: 'no-public' }],
'@typescript-eslint/indent': ['error', 'tab'],
'prettier/prettier': 'error',
},
};
5 changes: 3 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"printWidth": 120,
"tabWidth": 2,
"singleQuote": true,
"jsxBracketSameLine": true,
"bracketSameLine": true,
"trailingComma": "es5",
"arrowParens": "avoid"
"arrowParens": "avoid",
"endOfLine": "auto"
}
2 changes: 1 addition & 1 deletion build/setup/index.map.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ module.exports = {
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
'^.+\\.ts$': 'ts-jest',
},
};
8 changes: 4 additions & 4 deletions ncc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ async function build(file) {
const name = path.basename(file, path.extname(file));
const dir = path.resolve(BUILD_DIR, name);

await fs.promises.mkdir(dir, { recursive: true })
await fs.promises.mkdir(dir, { recursive: true });

for (const asset in assets) {
await fs.promises.mkdir(path.join(dir, path.dirname(asset)), { recursive: true })
await fs.promises.mkdir(path.join(dir, path.dirname(asset)), { recursive: true });
await write(path.join(dir, asset), assets[asset].source);
}

Expand All @@ -36,12 +36,12 @@ async function build(file) {
}

async function main() {
const actionPath = path.resolve(__dirname, './src/actions')
const actionPath = path.resolve(__dirname, './src/actions');
const actionFiles = (await fs.promises.readdir(actionPath, { withFileTypes: true }))
.filter(entity => entity.isFile())
.map(entity => path.join(ACTION_DIR, entity.name));

return Promise.all(actionFiles.map(build))
return Promise.all(actionFiles.map(build));
}

main();
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"homepage": "https://github.com/expo/expo-github-action#readme",
"scripts": {
"test": "jest",
"lint": "tsc --noEmit && eslint ./src ./tests",
"lint": "tsc --noEmit && eslint .",
"build": "node ncc.js",
"clean": "rimraf build",
"prepare": "husky install"
Expand All @@ -43,9 +43,11 @@
"conventional-changelog-conventionalcommits": "^4.6.0",
"eslint": "^8.2.0",
"eslint-plugin-jest": "^25.2.4",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^6.0.0",
"jest": "^27.3.1",
"jest-circus": "^27.3.1",
"prettier": "^2.4.1",
"rimraf": "^3.0.2",
"semantic-release": "^17.4.4",
"ts-jest": "^27.0.7",
Expand Down
25 changes: 12 additions & 13 deletions src/actions/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@ export async function setupAction(): Promise<void> {
const expoVersion = await installCli('expo-cli');
const easVersion = await installCli('eas-cli');

await group(
'Checking current authenticated account',
() => tools.maybeAuthenticate({
await group('Checking current authenticated account', () =>
tools.maybeAuthenticate({
cli: expoVersion ? 'expo-cli' : easVersion ? 'eas-cli' : undefined,
token: getInput('token') || undefined,
username: getInput('username') || undefined,
password: getInput('password') || undefined,
}),
})
);

if (tools.getBoolean(getInput('patch-watchers'), true)) {
await group(
'Patching system watchers for the `ENOSPC` error',
() => tools.maybePatchWatchers(),
);
await group('Patching system watchers for the `ENOSPC` error', () => tools.maybePatchWatchers());
}
}

Expand All @@ -45,11 +41,14 @@ async function installCli(name: tools.PackageName): Promise<string | void> {
cache
? `Installing ${name} (${version}) from cache or with ${packager}`
: `Installing ${name} (${version}) with ${packager}`,
() => install({
packager, version, cache,
package: name,
cacheKey: getInput(`${shortName}-cache-key`) || undefined,
}),
() =>
install({
packager,
version,
cache,
package: name,
cacheKey: getInput(`${shortName}-cache-key`) || undefined,
})
);

addPath(path);
Expand Down
4 changes: 1 addition & 3 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ function getRemoteKey(config: Omit<CacheConfig, 'cacheKey'>): string {
*/
function handleRemoteCacheError(error: Error): boolean {
const isReserveCacheError = error instanceof ReserveCacheError;
const isCacheUnavailable = error.message.toLowerCase().includes(
'cache service url not found',
);
const isCacheUnavailable = error.message.toLowerCase().includes('cache service url not found');

if (isReserveCacheError || isCacheUnavailable) {
core.warning('Skipping remote cache storage, encountered error:');
Expand Down
10 changes: 7 additions & 3 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ export async function maybeAuthenticate(options: AuthenticateOptions = {}): Prom
env: { ...process.env, EXPO_TOKEN: options.token },
});
} else {
core.info('Skipping token validation: no CLI installed, can\'t run `whoami`.');
core.info("Skipping token validation: no CLI installed, can't run `whoami`.");
}

return core.exportVariable('EXPO_TOKEN', options.token);
}

if (options.username || options.password) {
if (options.cli !== 'expo-cli') {
return core.warning('Skipping authentication: only Expo CLI supports programmatic credentials, use `token` instead.');
return core.warning(
'Skipping authentication: only Expo CLI supports programmatic credentials, use `token` instead.'
);
}

if (!options.username || !options.password) {
Expand Down Expand Up @@ -113,7 +115,9 @@ export async function maybeWarnForUpdate(name: PackageName): Promise<void> {

if (semver.diff(latest, current) === 'major') {
core.warning(`There is a new major version available of the Expo CLI (${latest})`);
core.warning(`If you run into issues, try upgrading your workflow to "${binaryName}-version: ${semver.major(latest)}.x"`);
core.warning(
`If you run into issues, try upgrading your workflow to "${binaryName}-version: ${semver.major(latest)}.x"`
);
}
}

Expand Down
7 changes: 3 additions & 4 deletions tests/actions/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ describe('run', () => {
mockInput({ [`${cliName}Version`]: 'latest' });
await setupAction();
expect(install.install).toBeCalledWith({
package:
packageName,
package: packageName,
version: 'latest',
packager: 'yarn',
cache: false,
Expand Down Expand Up @@ -101,7 +100,7 @@ describe('run', () => {
packager: 'yarn',
[`${cliName}Version`]: '4.2.0',
[`${cliName}Cache`]: 'true',
[`${cliName}CacheKey`]: 'custom-key'
[`${cliName}CacheKey`]: 'custom-key',
});
await setupAction();
expect(install.install).toBeCalledWith({
Expand Down Expand Up @@ -145,7 +144,7 @@ function mockInput(props: MockInputProps = {}) {
case 'expo-cache':
return props.expoCache || '';
case 'expo-cache-key':
return props.expoCacheKey || '';``
return props.expoCacheKey || '';
case 'eas-version':
return props.easVersion || '';
case 'eas-cache':
Expand Down
48 changes: 26 additions & 22 deletions tests/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe(cache.fromRemoteCache, () => {
expect(await cache.fromRemoteCache({ package: 'expo-cli', version: '3.20.1', packager: 'yarn' })).toBeUndefined();
expect(remoteCache.restoreCache).toBeCalledWith(
[join('cache', 'path', 'expo-cli', '3.20.1', os.arch())],
`expo-cli-${process.platform}-${os.arch()}-yarn-3.20.1`,
`expo-cli-${process.platform}-${os.arch()}-yarn-3.20.1`
);
});

Expand All @@ -68,30 +68,30 @@ describe(cache.fromRemoteCache, () => {
).toBeUndefined();
expect(remoteCache.restoreCache).toBeCalledWith(
[join('cache', 'path', 'eas-cli', '4.2.0', os.arch())],
'custom-cache-key',
'custom-cache-key'
);
});

it('returns path when remote cache exists', async () => {
spy.restore.mockResolvedValueOnce(true);
expect(
await cache.fromRemoteCache({ package: 'expo-cli', version: '3.20.1', packager: 'npm' })
).toBe(join('cache', 'path', 'expo-cli', '3.20.1', os.arch()));
expect(await cache.fromRemoteCache({ package: 'expo-cli', version: '3.20.1', packager: 'npm' })).toBe(
join('cache', 'path', 'expo-cli', '3.20.1', os.arch())
);
});

it('fails when remote cache throws', async () => {
const error = new Error('Remote cache restore failed');
spy.restore.mockRejectedValueOnce(error);
await expect(cache.fromRemoteCache({ package: 'eas-cli', version: '3.20.1', packager: 'yarn' })).rejects.toBe(error);
await expect(cache.fromRemoteCache({ package: 'eas-cli', version: '3.20.1', packager: 'yarn' })).rejects.toBe(
error
);
});

it('skips remote cache when unavailable', async () => {
// see: https://github.com/actions/toolkit/blob/9167ce1f3a32ad495fc1dbcb574c03c0e013ae53/packages/cache/src/internal/cacheHttpClient.ts#L41
const error = new Error('Cache Service Url not found, unable to restore cache.');
spy.restore.mockRejectedValueOnce(error);
expect(
await cache.fromRemoteCache({ package: 'expo-cli', version: '3.20.1', packager: 'yarn' })
).toBeUndefined();
expect(await cache.fromRemoteCache({ package: 'expo-cli', version: '3.20.1', packager: 'yarn' })).toBeUndefined();
expect(spy.warning).toHaveBeenCalledWith(expect.stringContaining('Skipping remote cache'));
});
});
Expand All @@ -112,38 +112,42 @@ describe(cache.toRemoteCache, () => {
});

it('saves remote cache with default key', async () => {
expect(await cache.toRemoteCache(join('local', 'path'), { package: 'eas-cli', version: '3.20.1', packager: 'npm' })).toBeUndefined();
expect(
await cache.toRemoteCache(join('local', 'path'), { package: 'eas-cli', version: '3.20.1', packager: 'npm' })
).toBeUndefined();
expect(remoteCache.saveCache).toBeCalledWith(
[join('local', 'path')],
`eas-cli-${process.platform}-${os.arch()}-npm-3.20.1`
);
});

it('saves remote cache with custom key', async () => {
expect(await cache.toRemoteCache(
join('local', 'path'),
{ package: 'eas-cli', version: '3.20.1', packager: 'yarn', cacheKey: 'custom-cache-key' }
)).toBeUndefined();
expect(
await cache.toRemoteCache(join('local', 'path'), {
package: 'eas-cli',
version: '3.20.1',
packager: 'yarn',
cacheKey: 'custom-cache-key',
})
).toBeUndefined();
expect(remoteCache.saveCache).toBeCalledWith([join('local', 'path')], 'custom-cache-key');
});

it('fails when remote cache throws', async () => {
const error = new Error('Remote cache save failed');
spy.save.mockRejectedValueOnce(error);
await expect(cache.toRemoteCache(
join('local', 'path'),
{ package: 'expo-cli', version: '3.20.1', packager: 'yarn' },
)).rejects.toBe(error);
await expect(
cache.toRemoteCache(join('local', 'path'), { package: 'expo-cli', version: '3.20.1', packager: 'yarn' })
).rejects.toBe(error);
});

it('skips remote cache when unavailable', async () => {
// see: https://github.com/actions/toolkit/blob/9167ce1f3a32ad495fc1dbcb574c03c0e013ae53/packages/cache/src/internal/cacheHttpClient.ts#L41
const error = new Error('Cache Service Url not found, unable to restore cache.');
spy.save.mockRejectedValueOnce(error);
await expect(cache.toRemoteCache(
join('local', 'path'),
{ package: 'expo-cli', version: '3.20.1', packager: 'yarn' },
)).resolves.toBeUndefined();
await expect(
cache.toRemoteCache(join('local', 'path'), { package: 'expo-cli', version: '3.20.1', packager: 'yarn' })
).resolves.toBeUndefined();
expect(spy.warning).toHaveBeenCalledWith(expect.stringContaining('Skipping remote cache'));
});
});
20 changes: 17 additions & 3 deletions tests/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ describe('install', () => {
cache.toLocalCache.mockResolvedValue(join('cache', 'path'));
const expoPath = await install.install({ package: 'expo-cli', version: '3.0.10', packager: 'npm' });
expect(expoPath).toBe(join('cache', 'path', 'node_modules', '.bin'));
expect(cache.toLocalCache).toBeCalledWith(join('temp', 'path'), { package: 'expo-cli', version: '3.0.10', packager: 'npm' });
expect(cache.toLocalCache).toBeCalledWith(join('temp', 'path'), {
package: 'expo-cli',
version: '3.0.10',
packager: 'npm',
});
utils.restoreEnv();
});

Expand All @@ -38,7 +42,12 @@ describe('install', () => {
cache.fromRemoteCache.mockResolvedValue(join('cache', 'path'));
const expoPath = await install.install({ package: 'eas-cli', version: '4.2.0', packager: 'yarn', cache: true });
expect(expoPath).toBe(join('cache', 'path', 'node_modules', '.bin'));
expect(cache.fromRemoteCache).toBeCalledWith({ package: 'eas-cli', version: '4.2.0', packager: 'yarn', cache: true });
expect(cache.fromRemoteCache).toBeCalledWith({
package: 'eas-cli',
version: '4.2.0',
packager: 'yarn',
cache: true,
});
});

it('installs path from packager and cache it remotely', async () => {
Expand All @@ -48,7 +57,12 @@ describe('install', () => {
cache.toLocalCache.mockResolvedValue(join('cache', 'path'));
const expoPath = await install.install({ package: 'expo-cli', version: '3.20.1', packager: 'npm', cache: true });
expect(expoPath).toBe(join('cache', 'path', 'node_modules', '.bin'));
expect(cache.toRemoteCache).toBeCalledWith(join('cache', 'path'), { package: 'expo-cli', version: '3.20.1', packager: 'npm', cache: true });
expect(cache.toRemoteCache).toBeCalledWith(join('cache', 'path'), {
package: 'expo-cli',
version: '3.20.1',
packager: 'npm',
cache: true,
});
utils.restoreEnv();
});
});
Expand Down
Loading

0 comments on commit 1cfa7bb

Please sign in to comment.