Add copy script
This commit is contained in:
@@ -0,0 +1,77 @@
|
|||||||
|
import { existsSync } from 'node:fs';
|
||||||
|
import { copyFile, mkdir, readdir } from 'node:fs/promises';
|
||||||
|
import { join } from 'node:path';
|
||||||
|
import prompts from 'prompts';
|
||||||
|
import { replaceInFile } from 'replace-in-file';
|
||||||
|
|
||||||
|
// Select year and date
|
||||||
|
const { year, day } = await prompts([
|
||||||
|
{
|
||||||
|
type: 'number',
|
||||||
|
name: 'year',
|
||||||
|
message: 'What year?',
|
||||||
|
initial: new Date().getFullYear(),
|
||||||
|
validate: (val) =>
|
||||||
|
val > 2000 && val < 3000 ? true : 'Year needs to be between 2000-3000',
|
||||||
|
format: String
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'number',
|
||||||
|
name: 'day',
|
||||||
|
message: 'What day?',
|
||||||
|
initial: new Date().getDate(),
|
||||||
|
validate: (val) =>
|
||||||
|
val > 0 && val < 32 ? true : 'Day needs to be between 1-31',
|
||||||
|
format: (val) => String(val).padStart(2, '0')
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
const dir = join(year, day);
|
||||||
|
|
||||||
|
if (existsSync(dir)) {
|
||||||
|
console.warn('Directory already exists!');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new day folder
|
||||||
|
await mkdir(dir, { recursive: true });
|
||||||
|
|
||||||
|
// Parse template folder for files
|
||||||
|
const templateDir = 'template';
|
||||||
|
const files = await readdir(templateDir);
|
||||||
|
|
||||||
|
const yearRegex = /YYYY/g;
|
||||||
|
const dayRegex = /DD/g;
|
||||||
|
|
||||||
|
// Copy files from template to day folder, replace placeholders in filename
|
||||||
|
await Promise.all(
|
||||||
|
files.map((f) =>
|
||||||
|
copyFile(
|
||||||
|
join(templateDir, f),
|
||||||
|
join(dir, f.replaceAll(yearRegex, year).replaceAll(dayRegex, day))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Replace placeholders in code
|
||||||
|
const resultYear = await replaceInFile({
|
||||||
|
files: [join(dir, '*')],
|
||||||
|
from: yearRegex,
|
||||||
|
to: year
|
||||||
|
});
|
||||||
|
const resultDay = await replaceInFile({
|
||||||
|
files: [join(dir, '*')],
|
||||||
|
from: dayRegex,
|
||||||
|
to: day
|
||||||
|
});
|
||||||
|
|
||||||
|
const results = [...resultYear, ...resultDay];
|
||||||
|
|
||||||
|
if (results.filter((r) => r.hasChanged).length <= 0) {
|
||||||
|
console.error(
|
||||||
|
'Something went wrong while replace placeholders! Did they change?'
|
||||||
|
);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Directory created! Have fun!');
|
||||||
+4
-1
@@ -10,8 +10,11 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bun": "latest",
|
"@types/bun": "latest",
|
||||||
|
"@types/prompts": "^2.4.9",
|
||||||
"prettier": "^3.4.0",
|
"prettier": "^3.4.0",
|
||||||
"prettier-plugin-organize-imports": "^4.1.0"
|
"prettier-plugin-organize-imports": "^4.1.0",
|
||||||
|
"prompts": "^2.4.2",
|
||||||
|
"replace-in-file": "^8.2.0"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"typescript": "^5.0.0"
|
"typescript": "^5.0.0"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
<component name="ProjectRunConfigurationManager">
|
||||||
<configuration default="false" name="Template" type="BunRunConfiguration">
|
<configuration default="false" name="YYYY-DD" type="BunRunConfiguration">
|
||||||
<option name="program" value="template/run.ts" />
|
<option name="program" value="YYYY/DD/run.ts" />
|
||||||
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
||||||
<method v="2" />
|
<method v="2" />
|
||||||
</configuration>
|
</configuration>
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { describe, expect, it } from 'bun:test';
|
import { describe, expect, it } from 'bun:test';
|
||||||
import { solveFirst, solveSecond } from './index.ts';
|
import { solveFirst, solveSecond } from './index.ts';
|
||||||
|
|
||||||
describe('20xx-xx', () => {
|
describe('YYYY-DD', () => {
|
||||||
it('first', () => {
|
it('first', () => {
|
||||||
// TODO: add first input
|
// TODO: add first input
|
||||||
const testInput = ``;
|
const testInput = ``;
|
||||||
|
|||||||
Reference in New Issue
Block a user