This is the template project that's checked out and configured when you run the bando-up command from ljsthw-bandolier. This is where the code really lives.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
bandolier-template/commands/indexnow.js

71 lines
2.2 KiB

// you may not need all of these but they come up a lot
import fs from "fs";
import logging from '../lib/logging.js';
import { og_base_host } from "../client/config.js";
import axios from "axios";
import crypto from "crypto";
import { glob, write } from "../lib/builderator.js";
const log = logging.create(import.meta.url);
const KEY_SIZE = 32;
const DEFAULT_ENGINE = "https://yandex.com";
export const description = "Uses IndexNow.org to submit your site for reindex. You'll need to configure your IndexNow.org account before doing this."
// your command uses the npm package commander's options format
export const options = [
["--gen-key", "If you don't have a key this will generate one."],
["--key-size <size>", "Key size to use with --gen-key", KEY_SIZE],
["--key <string>", "key to use"],
["--url <string>", "URL to submit", og_base_host],
["--key-path <path>", "Path to where the [key].txt should be stored. Command will ensure it's there or create it if not."],
["--search-engine <string>", "Company to notify, they're required to tell others.", DEFAULT_ENGINE],
]
const random_hex = (size) => {
return crypto.randomBytes(size).toString("hex");
}
// handy function for checking things are good and aborting
const check = (test, fail_message) => {
if(!test) {
log.error(fail_message);
process.exit(1);
}
}
export const main = async (opts) => {
if(opts.genKey) {
console.log(random_hex(parseInt(opts.keySize, 10)));
} else {
check(opts.key, "--key is required.");
check(opts.url, "--url is required.");
const keyfile = `${opts.keyPath}/${opts.key}.txt`;
// confirm the key file is in the right place
if(opts.keyPath && !fs.existsSync(keyfile)) {
console.log("WRITING KEY FILE", keyfile);
write(keyfile, opts.key);
}
const in_url = `${opts.searchEngine}/indexnow`;
const request = {
method: 'get', url: in_url,
data: { url: opts.url, key: opts.key}
};
try {
const result = await axios(request);
console.log("STATUS", result.status, "TEXT", result.statusText);
} catch(error) {
console.error(error.message, "REQUEST", request);
process.exit(1);
}
}
process.exit(0);
}