Skip to content

Commit

Permalink
Merge pull request #99 from kjirou/consider-timezone-with-now-command
Browse files Browse the repository at this point in the history
nowコマンドへ設定値のタイムゾーンを参照させる
  • Loading branch information
kjirou authored Dec 3, 2024
2 parents 42b0134 + 862cd7a commit 6f40ec3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
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

0 comments on commit 6f40ec3

Please sign in to comment.