generated from react-component/trigger
-
Notifications
You must be signed in to change notification settings - Fork 5
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: support for command line #14
Open
Wxh16144
wants to merge
3
commits into
ant-design:master
Choose a base branch
from
Wxh16144-forks:wuxh/feature-cli
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
require('../lib/cli/index.js'); |
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,41 @@ | ||
/** | ||
* coped from https://ant.design/docs/react/server-side-rendering-cn | ||
* > npx @ant-design/static-style-extract@latest -i ./docs/examples/_example.tsx | ||
*/ | ||
import * as React from 'react'; | ||
import { ConfigProvider } from 'antd'; | ||
|
||
const testGreenColor = '#008000'; | ||
const testRedColor = '#ff0000'; | ||
|
||
// Not a React component (Pure function) | ||
export default (node) => ( | ||
<> | ||
<ConfigProvider | ||
theme={{ | ||
token: { | ||
colorBgBase: testGreenColor, | ||
}, | ||
}} | ||
> | ||
{node} | ||
</ConfigProvider> | ||
<ConfigProvider | ||
theme={{ | ||
token: { | ||
colorPrimary: testGreenColor, | ||
}, | ||
}} | ||
> | ||
<ConfigProvider | ||
theme={{ | ||
token: { | ||
colorBgBase: testRedColor, | ||
}, | ||
}} | ||
> | ||
{node} | ||
</ConfigProvider> | ||
</ConfigProvider> | ||
</> | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import c from 'picocolors' | ||
import pkg from '../../package.json' | ||
|
||
export const ARROW = c.cyan('→') | ||
export const CHECK = c.green('✔') | ||
export const CROSS = c.red('✘') | ||
export const WARN = c.yellow('ℹ') | ||
export const PREFIX = 'antd' | ||
|
||
export const cssinjsVersion = pkg.dependencies['@ant-design/cssinjs']; | ||
export const version = pkg.version; | ||
export const name = pkg.name; | ||
|
||
export const example = ` | ||
import * as React from 'react'; | ||
import { ConfigProvider } from 'antd'; | ||
export default (node) => React.createElement(ConfigProvider, null, node); | ||
`.trim(); |
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,55 @@ | ||
import process from 'process' | ||
import c from 'picocolors' | ||
import { hideBin } from 'yargs/helpers' | ||
import yargs from 'yargs' | ||
import { CROSS, version, name } from './constants' | ||
import { run } from './run' | ||
|
||
function header() { | ||
console.log(`\n${c.green(`${name} `)}${c.dim(`v${version}`)}`) | ||
} | ||
|
||
const instance = yargs(hideBin(process.argv)) | ||
.scriptName(name) | ||
.usage('') | ||
.command( | ||
'*', | ||
'Generate static css', | ||
args => args | ||
.option('output', { | ||
alias: 'o', | ||
description: 'Output file path', | ||
type: 'string', | ||
default: 'antd.min.css' | ||
}) | ||
.option('input', { | ||
alias: 'i', | ||
description: 'Input file path', | ||
type: 'string' | ||
}) | ||
.option('overwrite', { | ||
alias: 'w', | ||
description: 'Overwrite existing files', | ||
type: 'boolean' | ||
}) | ||
.help(), | ||
async (args) => { | ||
header() | ||
console.log() | ||
try { | ||
await run(args) | ||
} | ||
catch (error) { | ||
console.error(c.inverse(c.red(' Failed to generate '))) | ||
console.error(c.red(`${CROSS} ${String(error)}`)) | ||
process.exit(1) | ||
} | ||
}, | ||
) | ||
.showHelpOnFail(false) | ||
.alias('h', 'help') | ||
.version('version', version) | ||
.alias('v', 'version') | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions | ||
instance.help().argv |
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,76 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import c from 'picocolors' | ||
import { CHECK, WARN, PREFIX } from './constants' | ||
import { nanoid } from 'nanoid' | ||
import execa from 'execa'; | ||
|
||
export interface CliOptions { | ||
/** | ||
* @default `antd.min.css` | ||
*/ | ||
output?: string; | ||
input?: string; | ||
overwrite?: boolean; | ||
} | ||
|
||
const tsxPath = require.resolve('tsx/cli'); | ||
const coreFilePath = path.join(__dirname, '..'); | ||
|
||
export async function run(options: CliOptions = {}) { | ||
const { output = "antd.min.css", input = '', overwrite } = options; | ||
const OVERWRITE = !!process.env.ALLAY_OVERWRITE /** Can be used for CI */ || overwrite; | ||
|
||
const cwd = process.cwd() | ||
|
||
const outputFilePath = path.join(cwd, output); | ||
const inputFilePath = path.join(cwd, input); | ||
|
||
const userInputFileExits = input.length > 0 && fs.existsSync(inputFilePath) | ||
|
||
if (input.length > 0 && !userInputFileExits) { | ||
console.log(c.yellow(`${WARN} ${input.length ? c.bold(input) : 'input'} is not exists.`)); | ||
} | ||
|
||
if (fs.existsSync(outputFilePath) && !OVERWRITE) { | ||
throw new Error(`${output} is already exists.`); | ||
} | ||
|
||
const outputDir = path.dirname(outputFilePath); | ||
if (!fs.existsSync(outputDir)) { | ||
fs.mkdirSync(outputDir, { recursive: true }); | ||
} | ||
|
||
// ====== Generate temp file ====== | ||
const id = nanoid().replaceAll('-', '_'); | ||
const fileExt = userInputFileExits ? path.extname(inputFilePath) : '.js'; | ||
const internalFileName = `.temp_${PREFIX}_${id}${fileExt}`, | ||
internalVariable = `${PREFIX}_${id}`; | ||
let internalFileContent = ''; | ||
|
||
if (userInputFileExits) { | ||
internalFileContent += `import ${internalVariable} from ${JSON.stringify(inputFilePath)};\n` | ||
} else { | ||
internalFileContent += `const ${internalVariable} = void 0;\n` | ||
} | ||
|
||
internalFileContent += ` | ||
import { extractStyle } from ${JSON.stringify(coreFilePath)}; | ||
import fs from 'fs'; | ||
fs.writeFileSync(${JSON.stringify(outputFilePath)}, extractStyle(${internalVariable})); | ||
`.trim(); | ||
|
||
const tmpFilePath = path.join(os.tmpdir(), internalFileName); | ||
const symlinkPath = path.join(path.dirname(inputFilePath), internalFileName); | ||
fs.writeFileSync(tmpFilePath, internalFileContent, { encoding: 'utf-8', mode: 0o777 }); | ||
fs.symlinkSync(tmpFilePath, symlinkPath, 'file'); | ||
|
||
execa.node(tsxPath, [symlinkPath]) | ||
.then(() => { | ||
console.log(c.green(`${CHECK} ${c.bold(output)} is generated.`)); | ||
}) | ||
.finally(() => { | ||
fs.unlinkSync(symlinkPath); | ||
}) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这个空的 log 拿来干嘛的?
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.
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.
空行.
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.
直接在打印的地方加
\n
不就行了么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.
条条大路通北京。😄
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.
感觉没必要单独写一个空 log ...
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.
哈哈哈 细枝末节
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.
整体看起来没啥大问题,可以测一下平台兼容性,至少 mac 和 windows 得支持