Skip to content

Commit

Permalink
feat(sample): prologue & epilogue
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Sep 21, 2024
1 parent 2c182c9 commit ae8aa3d
Show file tree
Hide file tree
Showing 17 changed files with 184 additions and 113 deletions.
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
shamefully-hoist = true

registry = https://mirrors.tencent.com/npm/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"packages/*"
],
"dependencies": {
"playwright-core": "^1.47.1"
"playwright-core": "~1.47.2"
},
"devDependencies": {
"@nice-move/all-in-base": "^0.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"cheetor": "^0.13.0"
},
"engines": {
"node": ">=22.0.0 || ^18.20.0 || ^20.4.0"
"node": ">=22.0.0 || ^18.20.0 || ^20.5.0"
},
"publishConfig": {
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion packages/notify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@bring-it/cli": "workspace:~"
},
"engines": {
"node": ">=22.0.0 || ^18.20.0 || ^20.4.0"
"node": ">=22.0.0 || ^18.20.0 || ^20.5.0"
},
"publishConfig": {
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion packages/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@bring-it/cli": "workspace:~"
},
"engines": {
"node": ">=22.0.0 || ^18.20.0 || ^20.4.0",
"node": ">=22.0.0 || ^18.20.0 || ^20.5.0",
"npm": "^10.5.0",
"pnpm": "^9.6.0"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ Config example:
"group": [
{
"cwd": ".",
"pattern": ["**/*"],
"prologue": ["prologue/*"],
"patterns": ["**/*"],
"epilogue": ["epilogue/*"],
"extensions": ["js", "ts", "..."],
"ignore": ["dist"],
"title": "示例软件名称",
Expand Down
6 changes: 3 additions & 3 deletions packages/sample/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bring-it/sample",
"version": "0.4.18",
"version": "0.4.19",
"description": "Generate code sample files",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -43,15 +43,15 @@
},
"peerDependencies": {
"@bring-it/cli": "workspace:~",
"playwright-core": ">=1.46.0"
"playwright-core": "~1.47.2"
},
"peerDependenciesMeta": {
"playwright-core": {
"optional": true
}
},
"engines": {
"node": ">=22.0.0 || ^18.20.0 || ^20.4.0"
"node": ">=22.0.0 || ^18.20.0 || ^20.5.0"
},
"publishConfig": {
"access": "public",
Expand Down
13 changes: 10 additions & 3 deletions packages/sample/src/lib/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export function mergeConfig(group = [{}]) {
version = 'v1.0',
cwd = '.',
pattern = ['**/*'],
patterns = pattern,
prologue = [],
epilogue = [],
ignore = ['dist'],
extensions = [
['js', 'cjs', 'mjs', 'jsx'],
Expand All @@ -43,16 +46,20 @@ export function mergeConfig(group = [{}]) {
checkString(title, `group[${index}].title`);
checkString(version, `group[${index}].version`);
checkString(cwd, `group[${index}].cwd`);
checkArray(pattern, `group[${index}].pattern`);
checkArray(patterns, `group[${index}].patterns`);
checkArray(prologue, `group[${index}].prologue`);
checkArray(epilogue, `group[${index}].epilogue`);
checkArray(extensions, `group[${index}].extensions`);
checkArray(ignore, `group[${index}].ignore`);

return {
title,
version,
cwd: resolve(process.cwd(), cwd),
pattern,
extensions,
patterns,
epilogue,
prologue,
extensions: extensions.map((extname) => `.${extname}`),
ignore,
};
},
Expand Down
14 changes: 11 additions & 3 deletions packages/sample/src/lib/pdf.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ function html([string]) {

const style = html`
<style>
header {
header,
footer {
display: flex;
margin: 0 5%;
margin: 1em 5%;
width: 92%;
font-size: 10pt;
font-family: 'Noto Sans Mono CJK SC', monospace;
Expand Down Expand Up @@ -53,7 +54,14 @@ export async function pdf(data, config) {
<span class="totalPages"></span>
</header>
` + style,
footerTemplate: ' ',
footerTemplate:
html`
<footer>
<div class="title"></div>
<span class="pageNumber"></span>/
<span class="totalPages"></span>
</footer>
` + style,
};

await page.waitForFunction(
Expand Down
118 changes: 46 additions & 72 deletions packages/sample/src/lib/picker.mjs
Original file line number Diff line number Diff line change
@@ -1,96 +1,70 @@
import { readFile } from 'node:fs/promises';
import { extname, join } from 'node:path';

import { globby } from 'globby';
import { logger } from './utils.mjs';

import { ignore as defaultIgnore } from '@bring-it/utils';
const LINE_NUMBERS = 3025;

import { logger } from './utils.mjs';
class Store extends Map {
constructor() {
super([
['prologue', []],
['patterns', []],
['epilogue', []],
]);
}

const lineBreak = /(\r\n|\n|\r)+/;
const lineBreakAll = /^(\r\n|\n|\r)+$/;
toLists() {
return [
...this.get('prologue'),
...this.get('patterns'),
...this.get('epilogue'),
].flat();
}

function read(file, config) {
return readFile(join(config.cwd, file), 'utf8');
length() {
return (
this.get('prologue').length +
this.get('patterns').length +
this.get('epilogue').length
);
}
}

const LINE_NUMBERS = 3050;
const lineBreak = /(\r\n|\n|\r)+/;

function readLine(file) {
return readFile(file.path, 'utf8').then((code) =>
code.split(lineBreak).filter((line) => line.trim() !== ''),
);
}

export async function picker(lists, config) {
const io = [];
async function mapper(io, key, lists) {
const patterns = lists[key];

for (const file of lists) {
if (io.length < LINE_NUMBERS) {
await read(file, config)
.then((code) =>
code
.split(lineBreak)
.filter((line) => !lineBreakAll.test(line))
.filter((line) => !/\s*\/\//.test(line)),
)
for (const file of patterns) {
if (LINE_NUMBERS > io.length()) {
await readLine(file)
.then((lines) => {
io.push(...lines);
logger.okay(file);
io.get(key).push(...lines.slice(0, LINE_NUMBERS - io.length()));

logger.okay(file.name);
})
.catch((error) => {
logger.fail(file);
logger.fail(file.name);
throw error;
});
} else {
break;
}
}

return io.join('\n').trim();
}

export function scan(config) {
return globby(config.pattern, {
cwd: config.cwd,
ignore: [
...defaultIgnore,
...config.ignore,
'**/.best-shot/**',
'**/.bring-it/**',
'**/.github/**',
'**/dist/**',
'**/License',
'**/License.*',
'**/*.md',
'**/.{gitattributes,gitignore,gitkeep}',
'**/.editorconfig',
'**/.npmrc',
'**/*.env.*',
'**/*.env',
'**/pnpm-lock.yaml',
'**/yarn.lock',
'**/package-lock.json',
],
gitignore: true,
onlyFiles: true,
dot: true,
caseSensitiveMatch: false,
})
.then((list) =>
config.extensions.length > 0
? list.filter((item) =>
config.extensions.includes(extname(item).replace(/^\./, '')),
)
: list,
)
.then((list) => {
if (list.length === 0) {
throw new Error('Not match anything');
}
export async function picker(lists) {
const io = new Store();

return list;
})
.then((list) => list.sort())
.then((list) => {
for (const item of list) {
logger.file(item);
}
await mapper(io, 'prologue', lists);
await mapper(io, 'epilogue', lists);
await mapper(io, 'patterns', lists);

return list;
});
return io.toLists().join('\n').trim();
}
5 changes: 3 additions & 2 deletions packages/sample/src/lib/sample.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { readConfig } from '@bring-it/utils';

import { mergeConfig } from './config.mjs';
import { pdf } from './pdf.mjs';
import { picker, scan } from './picker.mjs';
import { picker } from './picker.mjs';
import { scaner } from './scaner.mjs';
import { logger } from './utils.mjs';

export async function action() {
Expand All @@ -13,7 +14,7 @@ export async function action() {
logger.info(configs);

for (const config of configs) {
const files = await scan(config);
const files = await scaner(config);
const code = await picker(files, config);

const { title, version } = config;
Expand Down
78 changes: 78 additions & 0 deletions packages/sample/src/lib/scaner.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { extname, join } from 'node:path';

import { globby } from 'globby';

import { ignore as defaultIgnore } from '@bring-it/utils';

import { logger } from './utils.mjs';

const ignore = [
...defaultIgnore,
'**/.{gitattributes,gitkeep}',
'**/.*ignore',
'**/.best-shot/**',
'**/.bring-it/**',
'**/.editorconfig',
'**/.github/**',
'**/.npmrc',
'**/*.env.*',
'**/*.env',
'**/dist/**',
'**/License.*',
'**/License',
'**/package-lock.json',
'**/pnpm-lock.yaml',
'**/yarn.lock',
];

function find(key, config) {
const patterns = config[key];

return patterns.length > 0
? globby(patterns, {
cwd: config.cwd,
ignore: [...ignore, ...config.ignore],
gitignore: true,
onlyFiles: true,
dot: true,
caseSensitiveMatch: false,
})
.then((list) =>
config.extensions.length > 0
? list.filter((item) => config.extensions.includes(extname(item)))
: list,
)
.then((list) => list.sort())
.then((list) => {
for (const item of list) {
logger.log(`[${key}]`, item);
}

return list;
})
: [];
}

export async function scaner(config) {
const prologue = await find('prologue', config);
const epilogue = await find('epilogue', config);
const patterns = await find('patterns', config) //
.then((list) =>
list.filter(
(item) => !prologue.includes(item) && !epilogue.includes(item),
),
);

function wrapper(list) {
return list.map((name) => ({
name,
path: join(config.cwd, name),
}));
}

return {
prologue: wrapper(prologue),
epilogue: wrapper(epilogue),
patterns: wrapper(patterns),
};
}
Loading

0 comments on commit ae8aa3d

Please sign in to comment.