Skip to content

Commit

Permalink
feat: npx gulp new-pkg (DimensionDev#5668)
Browse files Browse the repository at this point in the history
* feat: npx gulp create-package

* feat: npx gulp create-package

* feat: npx gulp create-package

* feat: npx gulp create-package

* feat: npx gulp create-package

* chore: remove --deps option in clean script cause it does not work with swc

* feat: npx gulp create-package

* feat: npx gulp create-package

* feat: npx gulp create-package

* feat: npx gulp create-package

* feat: npx gulp create-package

* feat: npx gulp create-package

* feat: npx gulp create-package

* feat: npx gulp create-package

* docs: update docs

* feat: npx gulp create-package

* feat: npx gulp create-package

* fix: ci
  • Loading branch information
Jack-Works authored Feb 16, 2022
1 parent 0e3764a commit 9d0a712
Show file tree
Hide file tree
Showing 38 changed files with 500 additions and 160 deletions.
12 changes: 12 additions & 0 deletions .i18n-codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@
"trans": "Translate",
"sourceMap": "inline"
}
},
{
"input": "./packages/plugins/template/src/locales/en-US.json",
"output": "./packages/plugins/template/src/locales/i18n_generated",
"parser": "i18next",
"generator": {
"type": "i18next/react-hooks",
"hooks": "useI18N",
"namespace": "__template__",
"trans": "Translate",
"sourceMap": "inline"
}
}
]
}
2 changes: 0 additions & 2 deletions .pnpmfile.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const approvedList = new Map([
['html-parse-stringify2', 'github:locize/html-parse-stringify2'],
/* cspell:disable-next-line */
['async-eventemitter', 'github:ahultgren/async-eventemitter#fa06e39e56786ba541c180061dbf2c0a5bbf951c'],
// !!! Relative path
['xhr2-cookies', 'link:./package-overrides/xhr2-cookies'],
])

/**
Expand Down
24 changes: 11 additions & 13 deletions docs/plugin-development-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ maintainer:

# Plugin Development Guide

## Run

> npx gulp new-pkg
To create a new plugin

## Minimum File Structure

`packages/plugins/example/src` is an example of this structure.
The following folder is automatically created by the command above.

```plaintext
packages/mask/src/plugins/{your-plugin-name}
Expand All @@ -34,22 +40,14 @@ The file need to provide this information:
- Reference resource list
- Known issues / Caveats

## Registration

Import your plugin definition at: `packages/mask/src/plugin-infra/register.ts`.

If your plugin is defined at `packages/plugins/*` instead of `packages/mask/src/plugins/*`, please make sure you have set up the monorepo correctly.

Plugins defined at `packages/plugins/*` and compatible with the isolated dashboard should also be registered in `packages/dashboard/src/initialization/plugins.ts`

## Plugin APIs

- Plugin definition: `packages/plugin-infra/src/types.ts`
- Database: `context.getDatabaseStorage()` (2nd parameter of the `init` method of your `Worker` definition). See example in `packages/plugins/example/src/Worker/index.ts`.
- Message emitter: `createPluginMessage` in `packages/plugin-infra/src/utils/message.ts`
- RPC: `createPluginRPC` in `packages/plugin-infra/src/utils/rpc.ts`
- Metadata reader: `createTypedMessageMetadataReader` in `packages/mask/src/protocols/typed-message/metadata.ts`
- React renderer with metadata reader: `createRenderWithMetadata` in `packages/mask/src/protocols/typed-message/metadata.ts`
- Message emitter: `createPluginMessage` exported from `@masknet/plugin-infra`
- RPC: `createPluginRPC` exported from `@masknet/plugin-infra`
- Metadata reader: `createTypedMessageMetadataReader` in `@masknet/shared-base`
- React renderer with metadata reader: `createRenderWithMetadata` in `@masknet/shared-base`

## Architecture

Expand Down
1 change: 0 additions & 1 deletion package-overrides/xhr2-cookies/index.cjs

This file was deleted.

2 changes: 0 additions & 2 deletions package-overrides/xhr2-cookies/index.mjs

This file was deleted.

6 changes: 0 additions & 6 deletions package-overrides/xhr2-cookies/package.json

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@
},
"pnpm": {
"overrides": {
"xhr2-cookies": "link:./package-overrides/xhr2-cookies",
"@types/node": "^16.11.6"
"@types/node": "^17.0.17"
}
}
}
1 change: 0 additions & 1 deletion packages/empty/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "@masknet/null",
"version": "0.0.0",
"private": true,
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
Empty file added packages/empty/tests/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions packages/empty/tsconfig.tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"tsBuildInfoFile": "./dist/tests.tsbuildinfo",
"types": ["node"]
},
"include": ["./tests"],
"references": [{ "path": "./tsconfig.json" }]
}
27 changes: 21 additions & 6 deletions packages/mask/src/plugins/NextID/locales/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@
// Default fallback language in a family of languages are chosen by the alphabet order
// To overwrite this, please overwrite packages/scripts/src/locale-kit-next/index.ts
import en_US from './en-US.json'
import ja_JP from './ja-JP.json'
import ko_KR from './ko-KR.json'
import qya_AA from './qya-AA.json'
import zh_CN from './zh-CN.json'
import zh_TW from './zh-TW.json'
export const languages = {
en: en_US,
ja: ja_JP,
ko: ko_KR,
qy: qya_AA,
'zh-CN': zh_CN,
zh: zh_TW,
}
// @ts-ignore
if (import.meta.webpackHot) {
// @ts-ignore
import.meta.webpackHot.accept(['./en-US.json'], () =>
globalThis.dispatchEvent?.(
new CustomEvent('MASK_I18N_HMR', {
detail: ['com.mask.nextid', { en: en_US }],
}),
),
import.meta.webpackHot.accept(
['./en-US.json', './ja-JP.json', './ko-KR.json', './qya-AA.json', './zh-CN.json', './zh-TW.json'],
() =>
globalThis.dispatchEvent?.(
new CustomEvent('MASK_I18N_HMR', {
detail: [
'com.mask.next_id',
{ en: en_US, ja: ja_JP, ko: ko_KR, qy: qya_AA, 'zh-CN': zh_CN, zh: zh_TW },
],
}),
),
)
}
1 change: 1 addition & 0 deletions packages/plugin-infra/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ export enum PluginId {
PoolTogether = 'com.pooltogether',
UnlockProtocol = 'com.maskbook.unlockprotocol',
FileService = 'com.maskbook.fileservice',
// @masknet/scripts: insert-here
}

export interface Pagination {
Expand Down
6 changes: 2 additions & 4 deletions packages/plugins/example/src/locales/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
// Default fallback language in a family of languages are chosen by the alphabet order
// To overwrite this, please overwrite packages/scripts/src/locale-kit-next/index.ts
import en_US from './en-US.json'
import zh_TW from './zh-TW.json'
export const languages = {
en: en_US,
zh: zh_TW,
}
// @ts-ignore
if (import.meta.webpackHot) {
// @ts-ignore
import.meta.webpackHot.accept(['./en-US.json', './zh-TW.json'], () =>
import.meta.webpackHot.accept(['./en-US.json'], () =>
globalThis.dispatchEvent?.(
new CustomEvent('MASK_I18N_HMR', {
detail: ['io.mask.example', { en: en_US, zh: zh_TW }],
detail: ['io.mask.example', { en: en_US }],
}),
),
)
Expand Down
4 changes: 0 additions & 4 deletions packages/plugins/example/src/locales/zh-TW.json

This file was deleted.

7 changes: 7 additions & 0 deletions packages/plugins/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Example plugin

## TODOs

## Referenced resources

## Known issues / Caveats
12 changes: 12 additions & 0 deletions packages/plugins/template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@masknet/plugin-template",
"private": true,
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"dependencies": {
"@masknet/plugin-infra": "workspace:*",
"@masknet/shared": "workspace:*",
"@masknet/shared-base": "workspace:*",
"react-use": "^17.3.2"
}
}
9 changes: 9 additions & 0 deletions packages/plugins/template/src/Dashboard/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Plugin } from '@masknet/plugin-infra'
import { base } from '../base'

const dashboard: Plugin.Dashboard.Definition = {
...base,
init(signal, context) {},
}

export default dashboard
9 changes: 9 additions & 0 deletions packages/plugins/template/src/SNSAdaptor/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Plugin } from '@masknet/plugin-infra'
import { base } from '../base'

const sns: Plugin.SNSAdaptor.Definition = {
...base,
init(signal, context) {},
}

export default sns
9 changes: 9 additions & 0 deletions packages/plugins/template/src/Worker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Plugin } from '@masknet/plugin-infra'
import { base } from '../base'

const worker: Plugin.Worker.Definition = {
...base,
init(signal, context) {},
}

export default worker
18 changes: 18 additions & 0 deletions packages/plugins/template/src/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Plugin } from '@masknet/plugin-infra'
import { PLUGIN_DESCRIPTION, PLUGIN_ID, PLUGIN_NAME } from './constants'
import { languages } from './locales/languages'

export const base: Plugin.Shared.Definition = {
ID: PLUGIN_ID,
icon: '\u{1F9EA}',
name: { fallback: PLUGIN_NAME },
description: { fallback: PLUGIN_DESCRIPTION },
publisher: { name: { fallback: '' }, link: '' },
enableRequirement: {
architecture: { app: false, web: true },
networks: { type: 'opt-out', networks: {} },
target: 'beta',
},
experimentalMark: true,
i18n: languages,
}
5 changes: 5 additions & 0 deletions packages/plugins/template/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PluginId } from '@masknet/plugin-infra'

export const PLUGIN_ID = PluginId.Example
export const PLUGIN_DESCRIPTION = 'An example plugin of Mask Network.'
export const PLUGIN_NAME = 'Example'
1 change: 1 addition & 0 deletions packages/plugins/template/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@masknet/global-types/webpack" />
23 changes: 23 additions & 0 deletions packages/plugins/template/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { registerPlugin } from '@masknet/plugin-infra'
import { base } from './base'

export * from './constants'

registerPlugin({
...base,
SNSAdaptor: {
load: () => import('./SNSAdaptor'),
hotModuleReload: (hot) =>
import.meta.webpackHot && import.meta.webpackHot.accept('./SNSAdaptor', () => hot(import('./SNSAdaptor'))),
},
Dashboard: {
load: () => import('./Dashboard'),
hotModuleReload: (hot) =>
import.meta.webpackHot && import.meta.webpackHot.accept('./Dashboard', () => hot(import('./Dashboard'))),
},
Worker: {
load: () => import('./Worker'),
hotModuleReload: (hot) =>
import.meta.webpackHot && import.meta.webpackHot.accept('./Worker', () => hot(import('./Worker'))),
},
})
4 changes: 4 additions & 0 deletions packages/plugins/template/src/locales/en-US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Example Plugin",
"__entry__": "🤔 Example"
}
6 changes: 6 additions & 0 deletions packages/plugins/template/src/locales/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file is auto generated. DO NOT EDIT
// Run `npx gulp sync-languages` to regenerate.
// Default fallback language in a family of languages are chosen by the alphabet order
// To overwrite this, please overwrite packages/scripts/src/locale-kit-next/index.ts

export * from './i18n_generated'
21 changes: 21 additions & 0 deletions packages/plugins/template/src/locales/languages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file is auto generated. DO NOT EDIT
// Run `npx gulp sync-languages` to regenerate.
// Default fallback language in a family of languages are chosen by the alphabet order
// To overwrite this, please overwrite packages/scripts/src/locale-kit-next/index.ts
import en_US from './en-US.json'
export const languages = {
en: en_US,
}
import { createI18NBundle } from '@masknet/shared-base'
export const add__template__I18N = createI18NBundle('__template__', languages)
// @ts-ignore
if (import.meta.webpackHot) {
// @ts-ignore
import.meta.webpackHot.accept(['./en-US.json'], () =>
globalThis.dispatchEvent?.(
new CustomEvent('MASK_I18N_HMR', {
detail: ['__template__', { en: en_US }],
}),
),
)
}
11 changes: 11 additions & 0 deletions packages/plugins/template/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/.tsbuildinfo",
"resolveJsonModule": true
},
"include": ["src", "src/**/*.json"],
"references": [{ "path": "../../plugin-infra" }, { "path": "../../shared" }, { "path": "../../shared-base" }]
}
1 change: 1 addition & 0 deletions packages/plugins/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../../tsconfig.json",
"references": [
{ "path": "./template/" },
{ "path": "./example/" },
{ "path": "./Debugger/" },
{ "path": "./Flow/" },
Expand Down
3 changes: 3 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
"dependencies": {
"@types/rimraf": "^3.0.2",
"chalk": "^4.1.2",
"enquirer": "^2.3.6",
"fs-extra": "^10.0.0",
"gulp-zip": "^5.1.0",
"proper-lockfile": "^4.1.1",
"rimraf": "^3.0.2",
"yargs": "^17.3.1"
},
"devDependencies": {
"@types/fs-extra": "^9.0.13",
"@types/gulp": "^4.0.9",
"@types/lodash": "^4.14.178",
"@types/proper-lockfile": "^4.1.2",
Expand Down
22 changes: 5 additions & 17 deletions packages/scripts/src/commands/clean.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import { awaitChildProcess, parseArgs, printShell, ROOT_PATH, shell, task } from '../utils'
import { awaitChildProcess, printShell, ROOT_PATH, shell, task } from '../utils'
import rimraf from 'rimraf'
import { join } from 'path'
import { promisify } from 'util'
const rm = promisify(rimraf)

interface Args {
deps: boolean
}
export async function clean() {
const { deps } = parseArgs<Args>()
if (deps) {
printShell`rm -rf **/node_modules`
await rm(join(ROOT_PATH, '**/node_modules'))
await awaitChildProcess(shell.cwd(ROOT_PATH)`git clean -xdf ./packages/`)
} else {
await awaitChildProcess(shell.cwd(ROOT_PATH)`git clean -xdf -e node_modules ./packages/`)
printShell`rm -rf **/node_modules/.cache`
await rm(join(ROOT_PATH, '**/node_modules/.cache'))
}
await awaitChildProcess(shell.cwd(ROOT_PATH)`git clean -xdf -e node_modules ./packages/`)
printShell`rm -rf **/node_modules/.cache`
await rm(join(ROOT_PATH, '**/node_modules/.cache'))
await awaitChildProcess(shell.cwd(ROOT_PATH)`pnpm install --frozen-lockfile --prefer-offline`)
}

task(clean, 'clean', 'Clean all build caches', {
'--deps': 'including node_modules',
})
task(clean, 'clean', 'Clean all build caches')
1 change: 1 addition & 0 deletions packages/scripts/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './clean'
export * from './help'
export * from './new-package'
Loading

0 comments on commit 9d0a712

Please sign in to comment.