Skip to content

Commit c16358c

Browse files
committed
Update to 1.1.3
1 parent 17060da commit c16358c

File tree

8 files changed

+317
-25
lines changed

8 files changed

+317
-25
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
Все основные изменения задокументированы в этом файле.
44

5+
[1.1.3]: https://github.com/digikid/create-project/releases/tag/1.1.3
6+
7+
## [1.1.3] - 2022-02-09
8+
9+
### Исправлено
10+
- Исправлена ошибка форматирования файлов в ОС Windows
11+
- Из файла README.md удален значок с последней версией сборки
12+
- Рефакторинг кода, исправление мелких ошибок
13+
514
[1.1.2]: https://github.com/digikid/create-project/releases/tag/1.1.2
615

716
## [1.1.2] - 2022-02-08

lib/tasks/addConfig.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseRepoUrl } from '#lib/utils/path';
12
import { replaceAsync } from '#lib/utils/fs';
23
import { spinner } from '#lib/utils/log';
34

@@ -20,7 +21,8 @@ export default async (config, options) => {
2021

2122
const data = {};
2223
const repoUrl = `https://${repo.host.toLowerCase()}.com/${repo.owner}/${repo.name}`;
23-
const originalRepoName = `${originalRepoUrl}.git`.replace(/^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$/, '$5');
24+
25+
const originalRepo = parseRepoUrl(originalRepoUrl);
2426

2527
let description = '';
2628

@@ -95,8 +97,14 @@ export default async (config, options) => {
9597

9698
await replaceAsync({
9799
files: './README.md',
98-
from: originalRepoTitle,
99-
to: info.title
100+
from: [
101+
originalRepoTitle,
102+
/(\n+)(\!\[GitHub release])(.*)(\.svg\))(\n+)/gis
103+
],
104+
to: [
105+
info.title,
106+
'\n\n'
107+
]
100108
});
101109

102110
await replaceAsync({

lib/tasks/copyBoilerplate.js

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import path from 'path';
22
import del from 'del';
3+
import beautify from 'js-beautify';
34

4-
import { mergeDirsAsync, replaceAsync } from '#lib/utils/fs';
5+
import {
6+
mergeDirsAsync,
7+
replaceAsync,
8+
scanDirectory,
9+
readFileAsync,
10+
writeFileAsync
11+
} from '#lib/utils/fs';
512

13+
import { findDeep } from '#lib/utils/object';
614
import { spinner } from '#lib/utils/log';
715

816
export default async (config, options) => {
@@ -13,9 +21,7 @@ export default async (config, options) => {
1321
vendor
1422
} = options;
1523

16-
if (!files.boilerplate) {
17-
return;
18-
};
24+
if (!files.boilerplate) return;
1925

2026
const loader = spinner();
2127
const cwd = process.cwd();
@@ -24,8 +30,8 @@ export default async (config, options) => {
2430
const dest = path.join(cwd, 'src');
2531

2632
const paths = {
27-
styles: path.join(cwd, 'src/styles/**'),
28-
js: path.join(cwd, 'src/js/**')
33+
styles: path.join(cwd, 'src/styles'),
34+
js: path.join(cwd, 'src/js')
2935
};
3036

3137
const replaces = {
@@ -84,30 +90,57 @@ export default async (config, options) => {
8490
};
8591
});
8692

87-
addReplace(/(\r\n|\r|\n){1,}/g, '$1\n');
88-
addReplace(/^\s+\n/gm, '');
89-
addReplace(/(\{|,|\/\/.*)(\n+)/g, '$1\n');
90-
addReplace(/(\n+)(}|@?import)/g, '\n$2');
91-
addReplace(/;(\n+)(\s+)\$/g, ';\n\n$2\$');
92-
addReplace(/(\n+)(\s+)}/g, '\n$2}');
93+
const n = (process.platform === 'win32') ? '\r\n' : '\n';
9394

9495
try {
9596
loader.start('COPY_BOILERPLATE_START');
9697

98+
const formattedFiles = [];
99+
97100
await mergeDirsAsync(source, dest);
98101

102+
for (const path of Object.values(paths)) {
103+
const tree = scanDirectory(path);
104+
const files = findDeep(tree, ({ type }) => (type === 'file'));
105+
106+
for (const file of files) {
107+
const data = await readFileAsync(file.path);
108+
109+
if (data && data.includes('/* if')) {
110+
formattedFiles.push(file.path);
111+
};
112+
};
113+
};
114+
99115
await replaceAsync({
100116
files: [
101-
paths.styles,
102-
paths.js
117+
`${paths.styles}/**`,
118+
`${paths.js}/**`
103119
],
104120
...replaces
105121
});
106122

107-
if (removedFiles.length) {
108-
for (const file of removedFiles) {
109-
await del(path.join(cwd, file));
123+
for (const file of removedFiles) {
124+
await del(path.join(cwd, file));
125+
};
126+
127+
for (const file of formattedFiles) {
128+
const type = file.includes('.js') ? 'js' : 'css';
129+
130+
const data = await readFileAsync(file);
131+
132+
let formatted = beautify[type](data, {
133+
indent_size: 4,
134+
max_preserve_newlines: 2,
135+
preserve_newlines: true,
136+
brace_style: 'collapse,preserve-inline'
137+
});
138+
139+
if (type === 'js') {
140+
formatted = formatted.replace(/(,)(\n+)/gim, `$1${n}`).replace(/(\n+)(@?import)/gim, `${n}$2`);
110141
};
142+
143+
await writeFileAsync(file, formatted);
111144
};
112145

113146
loader.success('COPY_BOILERPLATE_SUCCESS');

lib/utils/fs.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ export const mergeDirsAsync = (source, dest) => {
7979

8080
export const scanDirectory = (path, options = {}) => {
8181
try {
82-
const result = dree.scan(path, options);
82+
const result = dree.scan(path, {
83+
showHidden: false,
84+
...options
85+
});
8386

8487
const tree = (result && ('children' in result)) ? result.children : [];
8588

lib/utils/object.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,28 @@ export const mergeDeep = (target, ...sources) => {
2424
};
2525

2626
return mergeDeep(target, ...sources);
27+
};
28+
29+
export const findDeep = (obj, cb) => {
30+
const keys = Object.keys(obj) || [];
31+
32+
let result = [];
33+
34+
if (cb(obj)) {
35+
result = [...result, obj];
36+
};
37+
38+
for (let i = 0; i < keys.length; i++) {
39+
let value = obj[keys[i]];
40+
41+
if (typeof value === 'object' && value != null) {
42+
let o = findDeep(value, cb);
43+
44+
if (o != null && o instanceof Array) {
45+
result = [...result, ...o];
46+
};
47+
};
48+
};
49+
50+
return result;
2751
};

lib/utils/path.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,21 @@ export const __dirname = path.dirname(fileURLToPath(import.meta.url));
66

77
export const dir = (child = '') => child ? path.join(__dirname, `../../${child}`) : __dirname;
88

9-
export const cwd = (child = '') => child ? path.join(process.cwd(), child) : process.cwd();
9+
export const cwd = (child = '') => child ? path.join(process.cwd(), child) : process.cwd();
10+
11+
export const parseRepoUrl = url => {
12+
const keys = ['protocol', 'separator', 'host', 'owner', 'name'];
13+
const reg = /^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$/;
14+
15+
const fullUrl = url.includes('.git') ? url : `${url}.git`;
16+
17+
const result = keys.reduce((acc, key) => {
18+
const index = keys.indexOf(key) + 1;
19+
20+
acc[key] = url ? fullUrl.replace(reg, `\$${index}`) : '';
21+
22+
return acc;
23+
}, {});
24+
25+
return result;
26+
};

0 commit comments

Comments
 (0)