-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
feat(publish): watch mode #24
base: develop
Are you sure you want to change the base?
Changes from 11 commits
bdeff90
fa3902c
3c96260
979dbf3
827ed96
90368a0
fc7fef2
9872dc2
430e51d
83c6ae5
4f52362
ebb6864
e0ad6af
16f75a3
cfc815d
4575527
5c5cb89
4a1bbb6
89c10f1
755d0af
b57fff4
8f145ba
47fc6eb
f2aed0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import path from 'path'; | ||
import fs from 'fs/promises'; | ||
import path from 'node:path'; | ||
import fs from 'node:fs/promises'; | ||
import outdent from 'outdent'; | ||
import { magenta, bold, dim } from 'kolorist'; | ||
import throttle from 'throttleit'; | ||
import globToRegexp from 'glob-to-regexp'; | ||
import { | ||
magenta, cyan, bold, dim, yellow, | ||
} from 'kolorist'; | ||
import { readPackageJson } from '../../utils/read-package-json.js'; | ||
import { getNpmPacklist } from '../../utils/get-npm-packlist.js'; | ||
import { cwdPath } from '../../utils/cwd-path.js'; | ||
import { getPrettyTime } from '../../utils/get-pretty-time.js'; | ||
import { hardlinkPackage } from './hardlink-package.js'; | ||
|
||
const isValidSetup = async ( | ||
|
@@ -27,6 +34,7 @@ const isValidSetup = async ( | |
export const linkPublishMode = async ( | ||
basePackagePath: string, | ||
linkPackagePath: string, | ||
watchMode?: boolean, | ||
) => { | ||
const absoluteLinkPackagePath = path.resolve(basePackagePath, linkPackagePath); | ||
const packageJson = await readPackageJson(absoluteLinkPackagePath); | ||
|
@@ -54,20 +62,68 @@ export const linkPublishMode = async ( | |
return; | ||
} | ||
|
||
/** | ||
* If it's a symlink, make sure it's in the node_modules directory of the base package. | ||
* e.g. This could happen with pnpm | ||
* | ||
* If it's not, it might be a development directory and we don't want to overwrite it. | ||
*/ | ||
const linkPathReal = await fs.realpath(linkPath); | ||
if (!linkPathReal.startsWith(expectedPrefix)) { | ||
return; | ||
} | ||
|
||
await hardlinkPackage( | ||
const throttledHardlinkPackage = throttle(hardlinkPackage, 500); | ||
await throttledHardlinkPackage( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not immediately clear to me why you chose to throttle this function. Maybe a code comment would be a good idea here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also was curious so I investigated a bit the difference between throttleit and p-throttle. I think |
||
linkPath, | ||
absoluteLinkPackagePath, | ||
packageJson, | ||
); | ||
|
||
if (watchMode) { | ||
const globOptions = { | ||
globstar: true, | ||
extended: true, | ||
}; | ||
|
||
/** | ||
* npm-packlist ignore list: | ||
* https://github.com/npm/npm-packlist/blob/v8.0.2/lib/index.js#L15-L38 | ||
*/ | ||
const ignoreFiles = [ | ||
// Files | ||
'**/{npm-debug.log,*.orig,package-lock.json,yarn.lock,pnpm-lock.yaml}', | ||
|
||
// Folders | ||
'**/node_modules/**', | ||
|
||
// Hidden files | ||
'**/.{_*,*.swp,DS_Store,gitignore,npmrc,npmignore,lock-wscript,.wafpickle-*}', | ||
|
||
// Hidden folders | ||
'**/.{_*,git,svn,hg,CVS}/**', | ||
].map(glob => globToRegexp(glob, globOptions)); | ||
|
||
const watcher = fs.watch( | ||
absoluteLinkPackagePath, | ||
{ recursive: true }, | ||
); | ||
|
||
for await (const { eventType, filename } of watcher) { | ||
if (!filename) { | ||
continue; | ||
} | ||
|
||
const shouldIgnore = ignoreFiles.some(ignoreFile => ignoreFile.test(filename)); | ||
if (shouldIgnore) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably doesn't matter too much, but one thing you could consider if you're sticking with diff --git a/src/commands/publish/link-publish-mode.ts b/src/commands/publish/link-publish-mode.ts
index e0b535c..69caad3 100644
--- a/src/commands/publish/link-publish-mode.ts
+++ b/src/commands/publish/link-publish-mode.ts
@@ -79,7 +79,7 @@ export const linkPublishMode = async (
* npm-packlist ignore list:
* https://github.com/npm/npm-packlist/blob/v8.0.2/lib/index.js#L15-L38
*/
- const ignoreFiles = [
+ const ignoreFiles = new RegExp([
// Files
'**/{npm-debug.log,*.orig,package-lock.json,yarn.lock,pnpm-lock.yaml}',
@@ -91,7 +91,8 @@ export const linkPublishMode = async (
// Hidden folders
'**/.{_*,git,svn,hg,CVS}/**',
- ].map(glob => globToRegexp(glob, globOptions));
+ ].map(glob => globToRegexp(glob, globOptions).source)
+ .join('|'));
const watcher = fs.watch(
absoluteLinkPackagePath,
@@ -103,7 +104,7 @@ export const linkPublishMode = async (
continue;
}
- const shouldIgnore = ignoreFiles.some(ignoreFile => ignoreFile.test(filename));
+ const shouldIgnore = ignoreFiles.test(filename);
if (shouldIgnore) {
continue;
}
|
||
continue; | ||
} | ||
|
||
const publishFiles = await getNpmPacklist( | ||
absoluteLinkPackagePath, | ||
packageJson, | ||
); | ||
|
||
if (!publishFiles.includes(filename)) { | ||
continue; | ||
} | ||
|
||
console.log(`\n${dim(getPrettyTime())}`, 'Detected', yellow(eventType), 'in', `${cyan(cwdPath(path.join(absoluteLinkPackagePath, filename)))}\n`); | ||
await throttledHardlinkPackage( | ||
linkPath, | ||
absoluteLinkPackagePath, | ||
packageJson, | ||
publishFiles, | ||
); | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const getPrettyTime = () => (new Date()).toLocaleTimeString( | ||
undefined, | ||
{ | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
hour12: true, | ||
}, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticed that this dependency is marked as archived/read-only, while a more popular alternative https://github.com/isaacs/minimatch is still actively maintained (and apparently used by npm internals).
Was there some reason you chose glob-to-regex instead?