Skip to content

Commit

Permalink
Merge pull request #6 from ckastbjerg/re-generate-types-no-file-add-a…
Browse files Browse the repository at this point in the history
…nd-remove

Re-generate types when files are added and/or removed
  • Loading branch information
ckastbjerg authored Mar 22, 2021
2 parents e2c3a06 + 5cf1f45 commit f0efcc3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 17 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-type-safe-routes",
"version": "0.0.21-alpha",
"version": "0.0.22-alpha",
"description": "Never should your users experience broken links again!",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -28,7 +28,8 @@
"dependencies": {
"@types/node": "^14.14.34",
"mkdirp": "^1.0.4",
"walk-sync": "^2.2.0"
"walk-sync": "^2.2.0",
"chokidar": "^3.5.1"
},
"devDependencies": {
"@types/jest": "^26.0.21",
Expand Down
40 changes: 31 additions & 9 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,42 @@ import generateTypeScriptFile from "./generateTypeScriptFile";

import mkdirp from "mkdirp";
import fs from "fs";
import chokidar from "chokidar";

const packageName = "next-type-safe-routes";
const typeFolder = `@types/${packageName}`;

const log = (message: string) => {
console.log(`\x1b[36m${packageName}\x1b[0m: ${message}`);
};

const writeTypesToDisc = (nextPagesDirectory: string) => {
const srcDir = nextPagesDirectory.replace("/pages", "");
const typeFolderPath = `${srcDir}/${typeFolder}`;
const typeScriptFile = generateTypeScriptFile(nextPagesDirectory);

mkdirp.sync(typeFolderPath);
fs.writeFileSync(`${typeFolderPath}/index.d.ts`, typeScriptFile);

log(`types written to ${typeFolder}`);
};

const run = (nextConfig = {}) => {
return Object.assign({}, nextConfig, {
webpack(config) {
// TODO: Is this the best way to get the pages dir?
// This seems to be the way to get the path to the pages
// directory in a Next.js app. Since it's possible to have a
// `/src` folder (https://nextjs.org/docs/advanced-features/src-directory)
// we cannot assume that it just in a `/pages` folder directly
// in the root of the project
const pagesDir = config.resolve.alias["private-next-pages"];
// It's allows to have a `src` folder in Next.js
// TODO: Is it possible to just get the src folder?
const srcDir = pagesDir.replace("/pages", "");
const typeFolder = `${srcDir}/@types/next-type-safe-routes`;
const typeScriptFile = generateTypeScriptFile(pagesDir);
mkdirp.sync(typeFolder);
fs.writeFileSync(`${typeFolder}/index.d.ts`, typeScriptFile);
console.info(`next-type-safe-routes: Wrote types to ${typeFolder}`);
// Generate the types file when the app is being compiled
writeTypesToDisc(pagesDir);
// Generate the types file again when page files are added/removed
const watcher = chokidar.watch(pagesDir, { ignoreInitial: true });
watcher.on("add", () => writeTypesToDisc(pagesDir));
watcher.on("unlink", () => writeTypesToDisc(pagesDir));
// Return the un-modified config
return config;
},
});
Expand Down
46 changes: 40 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ anymatch@^2.0.0:
micromatch "^3.1.4"
normalize-path "^2.1.1"

anymatch@^3.0.3:
anymatch@^3.0.3, anymatch@~3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.1.tgz#c55ecf02185e2469259399310c173ce31233b142"
integrity sha512-mM8522psRCqzV+6LhomX5wgp25YVibjh8Wj23I5RPkPppSVSjyKD2A2mBJmWGa+KN7f2D6LNh9jkBCeyLktzjg==
Expand Down Expand Up @@ -987,6 +987,11 @@ bcrypt-pbkdf@^1.0.0:
dependencies:
tweetnacl "^0.14.3"

binary-extensions@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand All @@ -1011,7 +1016,7 @@ braces@^2.3.1:
split-string "^3.0.2"
to-regex "^3.0.1"

braces@^3.0.1:
braces@^3.0.1, braces@~3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/braces/-/braces-3.0.2.tgz#3454e1a462ee8d599e236df336cd9ea4f8afe107"
integrity sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==
Expand Down Expand Up @@ -1122,6 +1127,21 @@ char-regex@^1.0.2:
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"
integrity sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==

chokidar@^3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.1.tgz#ee9ce7bbebd2b79f49f304799d5468e31e14e68a"
integrity sha512-9+s+Od+W0VJJzawDma/gvBNQqkTiqYTWLuZoyAsivsI4AaWTCzHG06/TMjsf1cYe9Cb97UCEhjz7HvnPk2p/tw==
dependencies:
anymatch "~3.1.1"
braces "~3.0.2"
glob-parent "~5.1.0"
is-binary-path "~2.1.0"
is-glob "~4.0.1"
normalize-path "~3.0.0"
readdirp "~3.5.0"
optionalDependencies:
fsevents "~2.3.1"

ci-info@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-2.0.0.tgz#67a9e964be31a51e15e5010d58e6f12834002f46"
Expand Down Expand Up @@ -1815,7 +1835,7 @@ fs.realpath@^1.0.0:
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=

fsevents@^2.1.2:
fsevents@^2.1.2, fsevents@~2.3.1:
version "2.3.2"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a"
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==
Expand Down Expand Up @@ -1871,7 +1891,7 @@ getpass@^0.1.1:
dependencies:
assert-plus "^1.0.0"

glob-parent@^5.0.0, glob-parent@^5.1.0:
glob-parent@^5.0.0, glob-parent@^5.1.0, glob-parent@~5.1.0:
version "5.1.2"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
integrity sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==
Expand Down Expand Up @@ -2093,6 +2113,13 @@ is-arrayish@^0.2.1:
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"
integrity sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=

is-binary-path@~2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-binary-path/-/is-binary-path-2.1.0.tgz#ea1f7f3b80f064236e83470f86c09c254fb45b09"
integrity sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==
dependencies:
binary-extensions "^2.0.0"

is-buffer@^1.1.5:
version "1.1.6"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
Expand Down Expand Up @@ -2176,7 +2203,7 @@ is-generator-fn@^2.0.0:
resolved "https://registry.yarnpkg.com/is-generator-fn/-/is-generator-fn-2.1.0.tgz#7d140adc389aaf3011a8f2a2a4cfa6faadffb118"
integrity sha512-cTIB4yPYL/Grw0EaSzASzg6bBy9gqCofvWN8okThAYIxKJZC+udlRAmGbM0XLeniEJSs8uEgHPGuHSe1XsOLSQ==

is-glob@^4.0.0, is-glob@^4.0.1:
is-glob@^4.0.0, is-glob@^4.0.1, is-glob@~4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.1.tgz#7567dbe9f2f5e2467bc77ab83c4a29482407a5dc"
integrity sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==
Expand Down Expand Up @@ -3055,7 +3082,7 @@ normalize-path@^2.1.1:
dependencies:
remove-trailing-separator "^1.0.1"

normalize-path@^3.0.0:
normalize-path@^3.0.0, normalize-path@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==
Expand Down Expand Up @@ -3357,6 +3384,13 @@ read-pkg@^5.2.0:
parse-json "^5.0.0"
type-fest "^0.6.0"

readdirp@~3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-3.5.0.tgz#9ba74c019b15d365278d2e91bb8c48d7b4d42c9e"
integrity sha512-cMhu7c/8rdhkHXWsY+osBhfSy0JikwpHK/5+imo+LpeasTF8ouErHrlYkwT0++njiyuDvc7OFY5T3ukvZ8qmFQ==
dependencies:
picomatch "^2.2.1"

regex-not@^1.0.0, regex-not@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/regex-not/-/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c"
Expand Down

0 comments on commit f0efcc3

Please sign in to comment.