Skip to content

Commit 3d32787

Browse files
authored
Lint (#132)
* Add github action * Fix lint violations
1 parent e6ba098 commit 3d32787

File tree

13 files changed

+155
-110
lines changed

13 files changed

+155
-110
lines changed

.github/workflows/test.yml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,35 @@ jobs:
9696
- name: Test
9797
run: npm run test
9898

99-
# Break the branch protection test into a seperate step, so we can manage the matrix more easily
99+
lint:
100+
runs-on: ubuntu-latest
101+
name: Lint
102+
steps:
103+
- uses: actions/checkout@v4
104+
- name: Setup node
105+
uses: actions/setup-node@v3
106+
with:
107+
node-version: ${{ matrix.node }}
108+
check-latest: true
109+
- name: node_modules cache
110+
id: node_modules_cache
111+
uses: actions/cache@v3
112+
with:
113+
path: ./node_modules
114+
key: ${{ runner.os }}-20-${{ hashFiles('package-lock.json') }}
115+
restore-keys: |
116+
${{ runner.os }}-20-
117+
- name: Install deps
118+
if: steps.node_modules_cache.outputs.cache-hit != 'true'
119+
run: npm ci
120+
- name: Test
121+
run: npm run lint:quiet
122+
123+
# Break the branch protection test into a separate step, so we can manage the matrix more easily
100124
test_and_contribute:
101125
runs-on: ubuntu-latest
102126
name: Branch protection
103-
needs: ['test']
127+
needs: ['test', 'lint']
104128
steps:
105129
- run: true
106130

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"test": "./scripts/test.js",
77
"publish": "./scripts/publish.js",
88
"lint": "eslint packages/* scripts/*",
9+
"lint:quiet": "eslint packages/* scripts/* --quiet",
910
"lint:fix": "eslint packages/* scripts/* --fix"
1011
},
1112
"workspaces": [

packages/@apphosting/adapter-angular/src/bin/create.spec.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,34 @@ const { readJson } = fsExtra;
77
const readPackageJson = readJson("package.json");
88
const importCreateJs = import("@apphosting/adapter-angular/dist/bin/create.js");
99

10-
describe("peer dependencies", async () => {
11-
12-
let expectedAngularRange: string;
13-
let exepctedDevKitArchitectRange: string;
14-
15-
before(async () => {
16-
const packageJson = await readPackageJson;
17-
const version = parse(packageJson.version);
18-
if (!version) throw "couldn't parse package.json version";
19-
expectedAngularRange = `~${version.major}.${version.minor}.0`;
20-
exepctedDevKitArchitectRange = `~0.${version.major}${version.minor < 10 ? '0' : ''}${version.minor}.0`;
21-
});
22-
23-
it("expected @angular/cli version requirement to match", async () => {
24-
const { ANGULAR_CLI_VERSION } = await importCreateJs;
25-
assert.equal(expectedAngularRange, ANGULAR_CLI_VERSION);
26-
});
27-
28-
it("expected @angular-devkit/architect version requirement to match", async () => {
29-
const packageJson = await readPackageJson;
30-
const devKitArchitectRange = packageJson.peerDependencies["@angular-devkit/architect"];
31-
assert.equal(exepctedDevKitArchitectRange, devKitArchitectRange);
32-
});
33-
34-
it("expected @angular-devkit/core version requirement to match", async () => {
35-
const packageJson = await readPackageJson;
36-
const devKitCoreRange = packageJson.peerDependencies["@angular-devkit/core"];
37-
assert.equal(expectedAngularRange, devKitCoreRange);
38-
});
10+
describe("peer dependencies", () => {
11+
let expectedAngularRange: string;
12+
let exepctedDevKitArchitectRange: string;
13+
14+
before(async () => {
15+
const packageJson = await readPackageJson;
16+
const version = parse(packageJson.version);
17+
if (!version) throw new Error("couldn't parse package.json version");
18+
expectedAngularRange = `~${version.major}.${version.minor}.0`;
19+
exepctedDevKitArchitectRange = `~0.${version.major}${version.minor < 10 ? "0" : ""}${
20+
version.minor
21+
}.0`;
22+
});
23+
24+
it("expected @angular/cli version requirement to match", async () => {
25+
const { ANGULAR_CLI_VERSION } = await importCreateJs;
26+
assert.equal(expectedAngularRange, ANGULAR_CLI_VERSION);
27+
});
28+
29+
it("expected @angular-devkit/architect version requirement to match", async () => {
30+
const packageJson = await readPackageJson;
31+
const devKitArchitectRange = packageJson.peerDependencies["@angular-devkit/architect"];
32+
assert.equal(exepctedDevKitArchitectRange, devKitArchitectRange);
33+
});
34+
35+
it("expected @angular-devkit/core version requirement to match", async () => {
36+
const packageJson = await readPackageJson;
37+
const devKitCoreRange = packageJson.peerDependencies["@angular-devkit/core"];
38+
assert.equal(expectedAngularRange, devKitCoreRange);
39+
});
3940
});

packages/@apphosting/adapter-nextjs/src/bin/build.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,44 @@ const publicDirectory = join(cwd, "public");
3232

3333
// Run build command
3434
function build(cwd: string) {
35-
spawnSync("npm", ["run", "build"], {cwd, shell: true, stdio: "inherit"});
35+
spawnSync("npm", ["run", "build"], { cwd, shell: true, stdio: "inherit" });
3636
}
3737

3838
// move public directory to apphosting output public directory
3939
const movePublicDirectory = async () => {
40-
const publicDirectoryExists = await exists(publicDirectory);
41-
if (!publicDirectoryExists) return;
42-
await move(publicDirectory, appHostingPublicDirectory, { overwrite: true });
40+
const publicDirectoryExists = await exists(publicDirectory);
41+
if (!publicDirectoryExists) return;
42+
await move(publicDirectory, appHostingPublicDirectory, { overwrite: true });
4343
};
4444

4545
// generate bundle.yaml
4646
const generateBundleYaml = async () => {
47-
const headers = manifest.headers.map(it => ({...it, regex: undefined}));
48-
const redirects = manifest.redirects.filter(it => !it.internal).map(it => ({...it, regex: undefined}));
49-
const beforeFileRewrites = Array.isArray(manifest.rewrites) ? manifest.rewrites : manifest.rewrites?.beforeFiles || [];
50-
const rewrites = beforeFileRewrites.map(it => ({...it, regex: undefined}));
51-
await writeFile(outputBundlePath, yamlStringify({
52-
headers,
53-
redirects,
54-
rewrites,
55-
runCommand: `node ${normalize(relative(cwd, serverFilePath))}`,
56-
neededDirs: [normalize(relative(cwd, appHostingOutputDirectory))],
57-
staticAssets: [normalize(relative(cwd, appHostingPublicDirectory))],
58-
}));
59-
}
47+
const headers = manifest.headers.map((it) => ({ ...it, regex: undefined }));
48+
const redirects = manifest.redirects
49+
.filter((it) => !it.internal)
50+
.map((it) => ({ ...it, regex: undefined }));
51+
const beforeFileRewrites = Array.isArray(manifest.rewrites)
52+
? manifest.rewrites
53+
: manifest.rewrites?.beforeFiles || [];
54+
const rewrites = beforeFileRewrites.map((it) => ({ ...it, regex: undefined }));
55+
await writeFile(
56+
outputBundlePath,
57+
yamlStringify({
58+
headers,
59+
redirects,
60+
rewrites,
61+
runCommand: `node ${normalize(relative(cwd, serverFilePath))}`,
62+
neededDirs: [normalize(relative(cwd, appHostingOutputDirectory))],
63+
staticAssets: [normalize(relative(cwd, appHostingPublicDirectory))],
64+
}),
65+
);
66+
};
6067

6168
// move the standalone directory, the static directory and the public directory to apphosting output directory
6269
// as well as generating bundle.yaml
6370
await move(standaloneDirectory, appHostingOutputDirectory, { overwrite: true });
6471
await Promise.all([
65-
move(staticDirectory, appHostingStaticDirectory, { overwrite: true }),
66-
movePublicDirectory(),
67-
generateBundleYaml(),
72+
move(staticDirectory, appHostingStaticDirectory, { overwrite: true }),
73+
movePublicDirectory(),
74+
generateBundleYaml(),
6875
]);

packages/@apphosting/adapter-nextjs/src/bin/create.spec.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,24 @@ const { readJson } = fsExtra;
77
const readPackageJson = readJson("package.json");
88
const importCreateJs = import("@apphosting/adapter-nextjs/dist/bin/create.js");
99

10-
describe("peer dependencies", async () => {
10+
describe("peer dependencies", () => {
11+
let expectedNextJSRange: string;
1112

12-
let expectedNextJSRange: string;
13+
before(async () => {
14+
const packageJson = await readPackageJson;
15+
const version = parse(packageJson.version);
16+
if (!version) throw new Error("couldn't parse package.json version");
17+
expectedNextJSRange = `~${version.major}.${version.minor}.0`;
18+
});
1319

14-
before(async () => {
15-
const packageJson = await readPackageJson;
16-
const version = parse(packageJson.version);
17-
if (!version) throw "couldn't parse package.json version";
18-
expectedNextJSRange = `~${version.major}.${version.minor}.0`;
19-
});
20+
it("expected create-next-app version requirement to match", async () => {
21+
const { CREATE_NEXT_APP_VERSION } = await importCreateJs;
22+
assert.equal(expectedNextJSRange, CREATE_NEXT_APP_VERSION);
23+
});
2024

21-
it("expected create-next-app version requirement to match", async () => {
22-
const { CREATE_NEXT_APP_VERSION } = await importCreateJs;
23-
assert.equal(expectedNextJSRange, CREATE_NEXT_APP_VERSION);
24-
});
25-
26-
it("expected next version requirement to match", async () => {
27-
const packageJson = await readPackageJson;
28-
const nextVersionRange = packageJson.peerDependencies["next"];
29-
assert.equal(expectedNextJSRange, nextVersionRange);
30-
});
25+
it("expected next version requirement to match", async () => {
26+
const packageJson = await readPackageJson;
27+
const nextVersionRange = packageJson.peerDependencies["next"];
28+
assert.equal(expectedNextJSRange, nextVersionRange);
29+
});
3130
});

packages/@apphosting/adapter-nextjs/src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {
2-
PHASE_PRODUCTION_BUILD as NEXT_PHASE_PRODUCTION_BUILD,
3-
ROUTES_MANIFEST as NEXT_ROUTES_MANIFEST,
2+
PHASE_PRODUCTION_BUILD as NEXT_PHASE_PRODUCTION_BUILD,
3+
ROUTES_MANIFEST as NEXT_ROUTES_MANIFEST,
44
} from "next/constants.js";
55

66
// export next/constants ourselves so we don't have to dynamically import them
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join } from "path";
22
import fsExtra from "fs-extra";
3-
import { PHASE_PRODUCTION_BUILD } from "./constants.js"
3+
import { PHASE_PRODUCTION_BUILD } from "./constants.js";
44
import { ROUTES_MANIFEST } from "./constants.js";
55
import { fileURLToPath } from "url";
66

@@ -10,19 +10,19 @@ import type { RoutesManifest } from "./interfaces.js";
1010
export const { readJson } = fsExtra;
1111

1212
export async function loadConfig(cwd: string) {
13-
// dynamically load NextJS so this can be used in an NPX context
14-
const { default: nextServerConfig }: { default: typeof import("next/dist/server/config.js") } =
15-
await import(`${cwd}/node_modules/next/dist/server/config.js`);
16-
const loadConfig = nextServerConfig.default;
17-
return await loadConfig(PHASE_PRODUCTION_BUILD, cwd);
13+
// dynamically load NextJS so this can be used in an NPX context
14+
const { default: nextServerConfig }: { default: typeof import("next/dist/server/config.js") } =
15+
await import(`${cwd}/node_modules/next/dist/server/config.js`);
16+
const loadConfig = nextServerConfig.default;
17+
return await loadConfig(PHASE_PRODUCTION_BUILD, cwd);
1818
}
1919

2020
export async function readRoutesManifest(distDir: string): Promise<RoutesManifest> {
21-
return await readJson(join(distDir, ROUTES_MANIFEST));
21+
return await readJson(join(distDir, ROUTES_MANIFEST));
2222
}
2323

2424
export const isMain = (meta: ImportMeta) => {
25-
if (!meta) return false;
26-
if (!process.argv[1]) return false;
27-
return process.argv[1] === fileURLToPath(meta.url);
25+
if (!meta) return false;
26+
if (!process.argv[1]) return false;
27+
return process.argv[1] === fileURLToPath(meta.url);
2828
};

packages/@apphosting/create/src/bin/create.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ import { spawn } from "child_process";
33
import { program } from "commander";
44

55
program
6-
.option('--framework <string>')
7-
.argument('<directory>', "path to the project's root directory")
8-
.action(async (directory, { framework }: { framework?: string }) => {
6+
.option("--framework <string>")
7+
.argument("<directory>", "path to the project's root directory")
8+
.action((directory, { framework }: { framework?: string }) => {
99
if (!framework) {
10-
throw new Error("Framework selecter not implemented. Must provide an option to --framework <string>");
10+
throw new Error(
11+
"Framework selecter not implemented. Must provide an option to --framework <string>",
12+
);
1113
}
1214
const adapterName = `@apphosting/adapter-${framework}`;
1315
const buildCommand = `apphosting-adapter-${framework}-create`;
14-
spawn('npx', ['-y', '-p', adapterName, buildCommand, directory], { shell: true, stdio: 'inherit' });
16+
spawn("npx", ["-y", "-p", adapterName, buildCommand, directory], {
17+
shell: true,
18+
stdio: "inherit",
19+
});
1520
});
1621

1722
program.parse();

packages/@apphosting/discover/src/index.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ type DiscoveredFramework = {
6161
bundledWith?: Array<(typeof PLATFORMS)[number][4][number][0]>;
6262
};
6363

64-
export async function discover(path: string, githubRepo?: string, githubToken?: string) {
64+
export async function discover(directory: string, githubRepo?: string, githubToken?: string) {
6565
if (githubRepo && !githubToken) throw new Error("needs token");
6666

67-
const { join } = await (githubRepo ? import("node:path") : import("node:path/posix"));
67+
const path = await (githubRepo ? import("node:path") : import("node:path/posix"));
6868

6969
const { readFile, pathExists, readJson } = githubRepo
7070
? {
@@ -115,12 +115,14 @@ export async function discover(path: string, githubRepo?: string, githubToken?:
115115
await Promise.all(
116116
PLATFORMS.map(
117117
async ([platform, files, defaultPackageManager, packageManagers, frameworkDefinitions]) => {
118-
const filesExist = await Promise.all(files.map((it) => pathExists(join(path, it))));
118+
const filesExist = await Promise.all(
119+
files.map((it) => pathExists(path.join(directory, it))),
120+
);
119121
if (files.length && !filesExist.some((it) => it)) return;
120122
const discoverFrameworks = (fallback = false) => {
121123
return async ([packageManager, possibleLockfiles]: (typeof packageManagers)[number]) => {
122124
const possibleLockfilesExist = await Promise.all(
123-
possibleLockfiles.map((it) => pathExists(join(path, it))),
125+
possibleLockfiles.map((it) => pathExists(path.join(directory, it))),
124126
);
125127
const [lockfile] = possibleLockfilesExist
126128
.map((exists, index) => (exists ? possibleLockfiles[index] : undefined))
@@ -131,7 +133,7 @@ export async function discover(path: string, githubRepo?: string, githubToken?:
131133
if (platform === "nodejs") {
132134
// TODO handle workspaces
133135
if (lockfile === "package-lock.json" || lockfile === "npm-shrinkwrap.json") {
134-
const packageJSON = await readJson(join(path, lockfile));
136+
const packageJSON = await readJson(path.join(directory, lockfile));
135137
packages = new Map(
136138
Object.keys(packageJSON.packages).map((pkg) => {
137139
const name = pkg.replace(/^node_modules\//, "");
@@ -140,7 +142,7 @@ export async function discover(path: string, githubRepo?: string, githubToken?:
140142
}),
141143
);
142144
} else if (lockfile === "yarn.lock") {
143-
const file = await readFile(join(path, lockfile));
145+
const file = await readFile(path.join(directory, lockfile));
144146
const yarnLock = YarnLockfile.parse(file.toString());
145147
if (yarnLock.type !== "success") throw new Error(`unable to read ${lockfile}`);
146148
packages = new Map(
@@ -151,7 +153,7 @@ export async function discover(path: string, githubRepo?: string, githubToken?:
151153
}),
152154
);
153155
} else if (lockfile === "pnpm-lock.yaml") {
154-
const file = await readFile(join(path, lockfile));
156+
const file = await readFile(path.join(directory, lockfile));
155157
const pnpmLock = parseYaml(file.toString());
156158
packages = new Map(
157159
Object.keys(pnpmLock.packages).map((pkg) => {
@@ -164,9 +166,11 @@ export async function discover(path: string, githubRepo?: string, githubToken?:
164166
} else if (platform === "python") {
165167
if (packageManager === "pip") {
166168
const requirementsFile = "requirements.txt";
167-
const requirementsFileExists = await pathExists(join(path, requirementsFile));
169+
const requirementsFileExists = await pathExists(
170+
path.join(directory, requirementsFile),
171+
);
168172
if (!requirementsFileExists) return false;
169-
const file = await readFile(join(path, requirementsFile));
173+
const file = await readFile(path.join(directory, requirementsFile));
170174
packages = new Map(
171175
file
172176
.toString()
@@ -176,7 +180,7 @@ export async function discover(path: string, githubRepo?: string, githubToken?:
176180
}),
177181
);
178182
} else if (lockfile === "Pipfile.lock") {
179-
const pipfileLock = await readJson(join(path, lockfile));
183+
const pipfileLock = await readJson(path.join(directory, lockfile));
180184
// TODO include develop too?
181185
packages = new Map(
182186
Object.keys(pipfileLock.default).map((name) => {
@@ -186,11 +190,11 @@ export async function discover(path: string, githubRepo?: string, githubToken?:
186190
}),
187191
);
188192
} else if (lockfile === "poetry.lock") {
189-
const poetryLock = await readFile(join(path, lockfile));
193+
const poetryLock = await readFile(path.join(directory, lockfile));
190194
packages = new Map(
191195
toml
192196
.parse(poetryLock.toString())
193-
.package?.map((it: any) => [it.name, it.version]),
197+
.package?.map((it: { name: string; version: string }) => [it.name, it.version]),
194198
);
195199
}
196200
}
@@ -200,9 +204,9 @@ export async function discover(path: string, githubRepo?: string, githubToken?:
200204
if (!requiredPackagePresent) continue;
201205
const requiredFileExist =
202206
requiredFiles.length === 0 ||
203-
(await Promise.all(requiredFiles.map((it) => pathExists(join(path, it))))).some(
204-
(it) => it,
205-
);
207+
(
208+
await Promise.all(requiredFiles.map((it) => pathExists(path.join(directory, it))))
209+
).some((it) => it);
206210
if (!requiredFileExist) continue;
207211
const [packageName] = requiredPackages;
208212
if (packageName)

0 commit comments

Comments
 (0)