Skip to content

Commit

Permalink
Update output path of user extension types
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Sep 1, 2024
1 parent 85b29bb commit 089f3aa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
12 changes: 5 additions & 7 deletions programs/create/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ export function isExternalTemplate(templateName: string) {
}

export function isTypeScriptTemplate(templateName: string) {
if (isExternalTemplate(templateName)) {
return false
}

return (
templateName === 'typescript' ||
templateName.startsWith('typescript-') ||
templateName.endsWith('-typescript')
templateName.includes('typescript') ||
templateName.includes('react') ||
templateName.includes('preact') ||
templateName.includes('svelte') ||
templateName.includes('solid')
)
}
12 changes: 6 additions & 6 deletions programs/create/steps/generate-extension-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export async function generateExtensionTypes(
const extensionEnvFile = path.join(projectPath, 'extension-env.d.ts')
const typePath =
process.env.EXTENSION_ENV === 'development'
? path.resolve(process.cwd(), 'programs/develop/types')
: 'extension-develop/dist/types'
? path.resolve(process.cwd(), 'programs/cli/types')
: 'extension/dist/types'

const fileContent = `\
// Required Extension.js types for TypeScript projects.
// This file auto-generated and should not be excluded.
// If you need extra types, consider creating a new *.d.ts and
// referencing it in the "include" array in your tsconfig.json file.
// See https://www.typescriptlang.org/tsconfig#include for info.
// This file is auto-generated and should not be excluded.
// If you need additional types, consider creating a new *.d.ts file and
// referencing it in the "include" array of your tsconfig.json file.
// See https://www.typescriptlang.org/tsconfig#include for more information.
/// <reference types="${typePath}/index.d.ts" />
// Polyfill types for browser.* APIs.
Expand Down
28 changes: 17 additions & 11 deletions programs/develop/commands/commands-lib/generate-extension-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@ import path from 'path'
import fs from 'fs/promises'
import * as messages from './messages'

export default async function generateExtensionTypes(projectDir: string) {
const extensionEnvFile = path.join(projectDir, 'extension-env.d.ts')
const typesFolderPath = path.join(__dirname, 'types')
const relativeTypePath = path.relative(projectDir, typesFolderPath)

export async function generateExtensionTypes(projectPath: string) {
const extensionEnvFile = path.join(projectPath, 'extension-env.d.ts')
const typePath =
process.env.EXTENSION_ENV === 'development'
? relativeTypePath
: `extension-develop/dist/types`
? path.resolve(process.cwd(), 'programs/cli/types')
: 'extension/dist/types'

const fileContent = `\
// Required Extension.js types for TypeScript projects.
// This file is auto-generated and should not be excluded.
// If you need extra types, consider creating a new *.d.ts and
// If you need additional types, consider creating a new *.d.ts file and
// referencing it in the "include" array of your tsconfig.json file.
// See https://www.typescriptlang.org/tsconfig#include for info.
// See https://www.typescriptlang.org/tsconfig#include for more information.
/// <reference types="${typePath}/index.d.ts" />
// Polyfill types for browser.* APIs.
Expand All @@ -34,14 +31,23 @@ export default async function generateExtensionTypes(projectDir: string) {
try {
// Check if the file exists
await fs.access(extensionEnvFile)

// Read the file content
const existingContent = await fs.readFile(extensionEnvFile, 'utf8')

// Check if the file contains the "develop/dist/types" string
if (existingContent.includes('develop/dist/types')) {
// Rewrite previous path for versions < 2.0.0. See #162
await fs.writeFile(extensionEnvFile, fileContent)
}
} catch (err) {
// File does not exist, continue to write it
const manifest = require(path.join(projectDir, 'manifest.json'))
const manifest = require(path.join(projectPath, 'manifest.json'))
console.log(messages.writingTypeDefinitions(manifest))
try {
await fs.writeFile(extensionEnvFile, fileContent)
} catch (writeErr) {
console.log(messages.writeTypeDefinitionsError(writeErr))
console.log(messages.writingTypeDefinitionsError(writeErr))
}
}
}
2 changes: 1 addition & 1 deletion programs/develop/commands/commands-lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export function writingTypeDefinitions(manifest: Manifest) {
)
}

export function writeTypeDefinitionsError(error: any) {
export function writingTypeDefinitionsError(error: any) {
return `${getLoggingPrefix(
'error'
)} Failed to write the extension type definition. ${red(error)}`
Expand Down
2 changes: 1 addition & 1 deletion programs/develop/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import fs from 'fs'
import path from 'path'
import {devServer} from '../webpack/dev-server'
import generateExtensionTypes from './commands-lib/generate-extension-types'
import {generateExtensionTypes} from './commands-lib/generate-extension-types'
import {isUsingTypeScript} from '../webpack/plugin-js-frameworks/js-tools/typescript'
import {getProjectPath} from './commands-lib/get-project-path'
import * as messages from './commands-lib/messages'
Expand Down

0 comments on commit 089f3aa

Please sign in to comment.