Skip to content
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

nowコマンドへ設定値のタイムゾーンを参照させる #99

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,19 @@ describe("executeHelp", () => {
});

describe("executeNow", () => {
beforeEach(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date("2019-01-01 00:00:00+0000"));
});

afterEach(() => {
jest.useRealTimers();
});

it("should return a date-time string formatted with UTC", () => {
return executeNow().then((result) => {
expect(result.exitCode).toBe(0);
expect(result.message).toBe("2019-01-01 00:00:00+0000");
});
test("タイムゾーンを考慮したDateTime文字列を返す", async () => {
jest.useFakeTimers();
jest.setSystemTime(new Date("2019-01-01 00:00:00+0000"));
await executeInit(workspaceRoot);
const configFilePath = path.join(workspaceRoot, "ubw-configs.js");
const result = await executeNow(configFilePath);
expect(result.exitCode).toBe(0);
// TODO: ubw-configs.js が Pure Object ではなく関数を返すので、timezone の値を Asia/Tokyo などにできない
// 他のタイムゾーンでテストしたい時は、現状は `createDefaultUbwConfigs` の戻り値を直接書き換える必要がある
expect(result.message).toBe("2019-01-01 00:00:00+0000");
});
});

Expand Down
8 changes: 6 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ const commandList: Record<string, Command> = {
);
return executeInit(destinationDirPath);
},
now: () => {
return executeNow();
now: ({ subCommandArgv, cwd, defaultConfigFilePath }) => {
const options = minimist(subCommandArgv);
const configFilePath = options["config-file"]
? cliUtils.toNormalizedAbsolutePath(options["config-file"], cwd)
: defaultConfigFilePath;
return executeNow(configFilePath);
},
unknown: () => {
return Promise.resolve({
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export function executeInit(blogRoot: string): Promise<CommandResult> {
const initialConfigs = createInitialUbwConfigs();
const configs = fillWithDefaultUbwConfigs(initialConfigs);

// TODO: 関数にしている理由が不明。ユニットテストで値を書き換えられなくて不便。
const configFileSource = [
"module.exports = function ubwConfigs() {",
`return ${JSON.stringify(initialConfigs, null, 2)};`
Expand Down Expand Up @@ -229,10 +230,11 @@ export function executeInit(blogRoot: string): Promise<CommandResult> {
});
}

export function executeNow(): Promise<CommandResult> {
export function executeNow(configFilePath: string): Promise<CommandResult> {
const settings = requireSettings(configFilePath);
return Promise.resolve({
exitCode: 0,
message: generateDateTimeString(new Date(), "UTC", {
message: generateDateTimeString(new Date(), settings.configs.timeZone, {
timeZoneSuffix: true,
}),
});
Expand Down