From f0ed7c0b39ef7add6868a2995ea8b42c0b8fb213 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Thu, 21 Apr 2022 12:42:57 +0200 Subject: [PATCH] #323 - Added additional SSGs --- CHANGELOG.md | 1 + src/constants/FrameworkDetectors.ts | 98 ++++++++++++++++++++++------- src/helpers/FrameworkDetector.ts | 40 +++++++++++- 3 files changed, 115 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 087d4369..73b04297 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - [#308](https://github.com/estruyf/vscode-front-matter/issues/308): New `File` field - [#314](https://github.com/estruyf/vscode-front-matter/issues/314): New preview actions to open the page in the browser and refresh the preview - [#322](https://github.com/estruyf/vscode-front-matter/issues/322): Show parent folder name when file is an index page (`index.md` / `_index.md`) +- [#323](https://github.com/estruyf/vscode-front-matter/issues/323): Added 11ty, jekyll, and docusaurus to the framework selection list ### ⚡️ Optimizations diff --git a/src/constants/FrameworkDetectors.ts b/src/constants/FrameworkDetectors.ts index b15a7925..76e9f488 100644 --- a/src/constants/FrameworkDetectors.ts +++ b/src/constants/FrameworkDetectors.ts @@ -1,33 +1,89 @@ -export const FrameworkDetectors = [ - { - "framework": {"name": "gatsby", "dist": "public", "static": "static", "build": "gatsby build"}, - "requiredFiles": ["gatsby-config.js"], - "requiredDependencies": ["gatsby"], - "commands": { - "start": "npx gatsby develop" +export const FrameworkDetectors = [{ + framework: { + name: "gatsby", + dist: "public", + static: "static", + build: "gatsby build" + }, + requiredFiles: ["gatsby-config.js"], + requiredDependencies: ["gatsby"], + commands: { + start: "npx gatsby develop" } }, { - "framework": {"name": "hugo", "dist": "public", "static": "static", "build": "hugo"}, - "requiredFiles": ["config.toml", "config.yaml", "config.yml"], - "commands": { - "start": "hugo server -D" + framework: { + name: "hugo", + dist: "public", + static: "static", + build: "hugo" + }, + requiredFiles: ["config.toml", "config.yaml", "config.yml"], + commands: { + start: "hugo server -D" } }, { - "framework": {"name": "next", "dist": ".next", "static": "public", "build": "next build"}, - "requiredFiles": ["next.config.js"], - "requiredDependencies": ["next"], - "commands": { - "start": "npx next dev" + framework: { + name: "next", + dist: ".next", + static: "public", + build: "next build" + }, + requiredFiles: ["next.config.js"], + requiredDependencies: ["next"], + commands: { + start: "npx next dev" } }, { - "framework": {"name": "nuxt", "dist": "dist", "static": "static", "build": "nuxt"}, - "requiredFiles": ["nuxt.config.js"], - "requiredDependencies": ["nuxt"], - "commands": { - "start": "npx nuxt" + framework: { + name: "nuxt", + dist: "dist", + static: "static", + build: "nuxt" + }, + requiredFiles: ["nuxt.config.js"], + requiredDependencies: ["nuxt"], + commands: { + start: "npx nuxt" + } + }, + { + framework: { + name: "jekyll", + dist: "_site", + static: "assets", + build: "bundle exec jekyll build" + }, + requiredFiles: ["Gemfile"], + requiredDependencies: ["jekyll"], + commands: { + start: "bundle exec jekyll serve --livereload" + } + }, + { + framework: { + name: "docusaurus", + dist: "build", + static: "static", + build: "npx docusaurus build" + }, + requiredFiles: ["docusaurus.config.js"], + requiredDependencies: ["@docusaurus/core"], + commands: { + start: "npx docusaurus start" + } + }, + { + framework: { + name: "11ty", + dist: "_site", + build: "npx @11ty/eleventy" + }, + requiredDependencies: ["@11ty/eleventy"], + commands: { + start: "npx @11ty/eleventy --serve" } } ]; \ No newline at end of file diff --git a/src/helpers/FrameworkDetector.ts b/src/helpers/FrameworkDetector.ts index cac2d469..9c2cf46a 100644 --- a/src/helpers/FrameworkDetector.ts +++ b/src/helpers/FrameworkDetector.ts @@ -1,5 +1,5 @@ -import { existsSync } from "fs"; -import { resolve } from "path"; +import { existsSync, readFileSync } from "fs"; +import { join, resolve } from "path"; import { FrameworkDetectors } from "../constants/FrameworkDetectors"; import { Extension } from "./Extension"; @@ -14,18 +14,52 @@ export class FrameworkDetector { } private static check(folder: string) { - const { dependencies, devDependencies } = Extension.getInstance().packageJson; + let dependencies = null; + let devDependencies = null; + let gemContent = null; + + // Try fetching the package JSON file + try { + const pkgFile = join(folder, 'package.json'); + if (existsSync(pkgFile)) { + let packageJson: any = readFileSync(pkgFile, "utf8"); + if (packageJson) { + packageJson = typeof packageJson === "string" ? JSON.parse(packageJson) : packageJson; + + dependencies = packageJson.dependencies || null; + devDependencies = packageJson.devDependencies || null; + } + } + } catch (e) { + // do nothing + } + + // Try fetching the Gemfile + try { + const gemFile = join(folder, 'Gemfile'); + if (existsSync(gemFile)) { + gemContent = readFileSync(gemFile, "utf8"); + } + } catch (e) { + // do nothing + } for (const detector of FrameworkDetectors) { if (detector && folder) { // Verify by dependencies for (const dependency of detector.requiredDependencies ?? []) { + // Checks for package.json dependencies const inDependencies = dependencies && dependencies[dependency] const inDevDependencies = devDependencies && devDependencies[dependency] if (inDependencies || inDevDependencies) { return detector.framework; } + + // Checks for Gemfile + if (gemContent && gemContent.includes(dependency)) { + return detector.framework; + } } // Verify by files