-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strapi): add experimental plugin:watch:link command to CLI (#19143)
* feat(strapi): add plugin:link-watch command to CLI * chore: skip assertion of strapi project if its the plugin:init command * fix: add yalc * fix: add yalc files/folders to ignore list to avoid restarting strapi when using * chore: don't use run-action on plugin:init * chore: add yarn dlx command to yarn suggestion
- Loading branch information
1 parent
a3690b1
commit e1ede8c
Showing
11 changed files
with
368 additions
and
20 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
docs/docs/docs/01-core/strapi/commands/plugin/05-link-watch.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: plugin:watch:link | ||
description: An in depth look at the plugin:watch:link command of the Strapi CLI | ||
tags: | ||
- CLI | ||
- commands | ||
- plugins | ||
--- | ||
|
||
The `plugin:watch:link` command recompiles your plugin and pushes those changes to your local yalc registry to simulate using your plugin as a node_module in another project. | ||
|
||
## Usage | ||
|
||
```bash | ||
strapi plugin:watch:link | ||
``` | ||
|
||
### Options | ||
|
||
```bash | ||
Recompiles your plugin automatically on changes and runs yalc push --publish | ||
|
||
Options: | ||
-d, --debug Enable debugging mode with verbose logs (default: false) | ||
--silent Don't log anything (default: false) | ||
-h, --help Display help for command | ||
``` | ||
## Why yalc? | ||
npm link & yarn link unfortunately can easily break the [rules of hooks](https://legacy.reactjs.org/docs/hooks-rules.html) due to the way packages are resolved using symlinks. | ||
Yalc bypass this problem as it more closely resembles installing a dependency as normal. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/core/strapi/src/commands/actions/plugin/link-watch/action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import boxen from 'boxen'; | ||
import chalk from 'chalk'; | ||
import concurrently from 'concurrently'; | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import nodemon from 'nodemon'; | ||
import { outdent } from 'outdent'; | ||
|
||
import { notifyExperimentalCommand } from '../../../utils/helpers'; | ||
import { CLIContext } from '../../../types'; | ||
import { loadPkg, validatePkg } from '../../../utils/pkg'; | ||
|
||
interface ActionOptions {} | ||
|
||
export default async (_opts: ActionOptions, _cmd: unknown, { cwd, logger }: CLIContext) => { | ||
try { | ||
/** | ||
* Notify users this is an experimental command. | ||
*/ | ||
await notifyExperimentalCommand('plugin:watch:link', { force: true }); | ||
|
||
const outDir = './dist'; | ||
const extensions = 'ts,js,png,svg,gif,jpeg,css'; | ||
|
||
nodemon({ | ||
watch: [outDir], | ||
ext: extensions, | ||
exec: 'yalc push --changed', | ||
}); | ||
|
||
const folder = path.join(cwd, outDir); | ||
|
||
if (!(await pathExists(folder))) { | ||
await fs.mkdir(folder); | ||
} | ||
|
||
const pkg = await loadPkg({ cwd, logger }); | ||
const pkgJson = await validatePkg({ pkg }); | ||
|
||
concurrently(['npm run watch']); | ||
|
||
nodemon | ||
.on('start', () => { | ||
logger.info( | ||
outdent` | ||
Watching ${outDir} for changes to files with extensions: ${extensions} | ||
To use this package in Strapi, in a separate shell run: | ||
cd /path/to/strapi/project | ||
Then run one of the commands below based on the package manager used in that project: | ||
## yarn | ||
${chalk.greenBright(`yarn dlx yalc add --link ${pkgJson.name} && yarn install`)} | ||
## npm | ||
${chalk.greenBright( | ||
`npx yalc add ${pkgJson.name} && npx yalc link ${pkgJson.name} && npm install` | ||
)} | ||
`.trimStart() | ||
); | ||
}) | ||
.on('quit', () => { | ||
process.exit(); | ||
}) | ||
.on('restart', (files) => { | ||
logger.info('Found changes in files:', chalk.magentaBright(files)); | ||
logger.info('Pushing new yalc package...'); | ||
}); | ||
} catch (err) { | ||
logger.error( | ||
'There seems to be an unexpected error, try again with --debug for more information \n' | ||
); | ||
if (err instanceof Error && err.stack) { | ||
console.log( | ||
chalk.red( | ||
boxen(err.stack, { | ||
padding: 1, | ||
align: 'left', | ||
}) | ||
) | ||
); | ||
} | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
const pathExists = async (path: string) => { | ||
try { | ||
await fs.access(path); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/core/strapi/src/commands/actions/plugin/link-watch/command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { StrapiCommand } from '../../../types'; | ||
import { runAction } from '../../../utils/helpers'; | ||
import action from './action'; | ||
|
||
/** | ||
* `$ strapi plugin:watch:link` | ||
*/ | ||
const command: StrapiCommand = ({ command, ctx }) => { | ||
command | ||
.command('plugin:watch:link') | ||
.description('Recompiles your plugin automatically on changes and runs yalc push --publish') | ||
.option('-d, --debug', 'Enable debugging mode with verbose logs', false) | ||
.option('--silent', "Don't log anything", false) | ||
.action((...args) => runAction('plugin:watch:link', action)(...args, ctx)); | ||
}; | ||
|
||
export default command; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.